optimizing threads

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
d1mbu1b
Posts: 6
Joined: Thu May 22, 2008 11:18 pm

optimizing threads

Post by d1mbu1b »

I am having trouble getting a threaded program to run efficiently.

When I break the code into two threads it runs much slower.


Please let me know if there is any specific tuning I should look into.
Do I need -DREENTRANT?
I am using the default pspsdk Makefile template.
There is no need for mutual exclusion in this code.
since the main thread only reads the data
and the loader thread only writes the data.
they are protected by the poor mans semaphore.
(an atomic pointer assignment)

Is there a better way to force the thread to relese the CPU then sceKernelDelayThread(100000);
I think I use the VFPU in the main thread which takes advantage of
pspgl, but the loader thread only performs IO

the following code is tested and runs correctly on the PSP.
Please point me in the right direction.

Code: Select all

int ChartsInit()
{
  fprintf(stderr, "sizeof Tile: %d\n", sizeof(Tile));

#ifdef __psp__
  //#define SINGLETHREAD
  #ifndef SINGLETHREAD
  int thid = 0;
  thid = sceKernelCreateThread("tile_load", ChartLoaderThread,
        0x18, 0xFA0, 0, 0);
  if (thid >= 0)
    sceKernelStartThread(thid, 0, 0);
  #endif
#else
  pthread_t thrd;
  pthread_create(&thrd, NULL, ChartLoaderThread, NULL);
#endif

  return(1);
}

I have what I think is a relatively low priority.



Code: Select all

void *ChartLoaderThread(void *dummy)
{
  Tile *t;
  int i;
  unsigned char *tmpBits;

#ifndef SINGLETHREAD
  while (1)
  {
    printf("+");
#ifdef __psp__
    sceKernelDelayThread(100000);
#else
    usleep(100000);
#endif

    if (p.vfrSectionals == 0)
      continue;
#endif

  
    // load 
    for &#40;i=0; i<numTiles; i++&#41;
    &#123;
      int c;
      t = &tiles&#91;i&#93;;k
      // this function opens a file from the flash drive and reads the 
      // pre-compiled texture
      if &#40;t->visible&#41; && &#40;t->bits == NULL&#41;&#41;
        LoadBits&#40;t&#41;;
    &#125;
#ifndef SINGLETHREAD
  &#125;
#endif
&#125;

running the thread causes the main thread to slow to a crawl
If I run single threaded and call this function in the main() it
executes correctly just a little too slow for my needs.

the dynamic memory is very regimented and controlled.
no memory leak on other platforms.

I was hoping to perform the file management in the background.
I figured file IO would release the CPU and switch to the main thread when waiting for the flash to respond.

I was expecting a significant increase in performance even with a single CPU

this same code runs well on my ipod and did yield the expected performance gain.

Each tile is about 769 Kbytes
Currently I am limiting it to one tile at a time for debugging purposes.

here is the file IO function

Code: Select all

&#123;
  FILE *fptr;
  int ret;
  unsigned char *tmpBits;

      /* open cache file */
      printf&#40;"load bits for %s\n", t->filename&#41;;
      fptr = fopen&#40;t->filename, "r"&#41;;
      if &#40;fptr == NULL&#41;
      &#123;
        printf&#40;"could not open %s\n", t->filename&#41;;
        //unlink&#40;t->filename&#41;;
        return;
      &#125;
      printf&#40;"opened file\n"&#41;;

      /* skip the tile structure - fix me with long word apadded fields */
      //fseek&#40;fptr, sizeof&#40;Tile&#41;, SEEK_SET&#41;;
      fseek&#40;fptr, 296, SEEK_SET&#41;;
      printf&#40;"skipped header\n"&#41;;

      /* allocate memory for pixmap */
      tmpBits = malloc&#40;3*C_TILE_X*C_TILE_Y&#41;;
      if &#40;tmpBits == NULL&#41;
      &#123;
        fprintf&#40;stderr, "could not allocate pixmap for cached tile\n"&#41;;
        fclose&#40;fptr&#41;;
        return;
      &#125;
      printf&#40;"allocated pixmap for cached tile\n"&#41;;

      /* read the pixmap */
      ret = fread&#40;tmpBits, 1, 3*C_TILE_X*C_TILE_Y, fptr&#41;;
      if &#40;ret != 3*C_TILE_X*C_TILE_Y&#41;
      &#123;
        fprintf&#40;stderr, "could not read cache tile\n"&#41;;
        fclose&#40;fptr&#41;;
        unlink&#40;t->filename&#41;;
        free&#40;tmpBits&#41;;
        t->bits = NULL;
        return;
      &#125;

      printf&#40;"read pixmap for cached tile\n"&#41;;
      t->bits = tmpBits;
      fclose&#40;fptr&#41;;
&#125;
Post Reply