forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

optimizing threads

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
d1mbu1b



Joined: 22 May 2008
Posts: 6

PostPosted: Tue Oct 28, 2008 11:27 pm    Post subject: optimizing threads Reply with quote

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:
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:
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 (i=0; i<numTiles; i++)
    {
      int c;
      t = &tiles[i];k
      // this function opens a file from the flash drive and reads the
      // pre-compiled texture
      if (t->visible) && (t->bits == NULL))
        LoadBits(t);
    }
#ifndef SINGLETHREAD
  }
#endif
}



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:
{
  FILE *fptr;
  int ret;
  unsigned char *tmpBits;

      /* open cache file */
      printf("load bits for %s\n", t->filename);
      fptr = fopen(t->filename, "r");
      if (fptr == NULL)
      {
        printf("could not open %s\n", t->filename);
        //unlink(t->filename);
        return;
      }
      printf("opened file\n");

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

      /* allocate memory for pixmap */
      tmpBits = malloc(3*C_TILE_X*C_TILE_Y);
      if (tmpBits == NULL)
      {
        fprintf(stderr, "could not allocate pixmap for cached tile\n");
        fclose(fptr);
        return;
      }
      printf("allocated pixmap for cached tile\n");

      /* read the pixmap */
      ret = fread(tmpBits, 1, 3*C_TILE_X*C_TILE_Y, fptr);
      if (ret != 3*C_TILE_X*C_TILE_Y)
      {
        fprintf(stderr, "could not read cache tile\n");
        fclose(fptr);
        unlink(t->filename);
        free(tmpBits);
        t->bits = NULL;
        return;
      }

      printf("read pixmap for cached tile\n");
      t->bits = tmpBits;
      fclose(fptr);
}
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group