Advertisement
Guest User

thread_test

a guest
Sep 28th, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. #include <SDL.h>
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. Uint32 tStart;
  8. void timerStart() {
  9.     Uint32 c = SDL_GetTicks();
  10.     while (c == SDL_GetTicks());
  11.     tStart = SDL_GetTicks();
  12. }
  13.  
  14. Uint32 timerEnd() {
  15.     return SDL_GetTicks() - tStart;
  16. }
  17.  
  18. void printTime(Uint32 timeMs) {
  19.     cout << (static_cast<float>(timeMs) / 1000.0f);
  20. }
  21.  
  22. const int   dataSizeCacheLines              = 8 * 1024 * 1024;
  23. const int   dataSize                        = 64 * dataSizeCacheLines;
  24. const int   cacheLineCountPerWorkThreadPull = 1024 / 64;
  25. const int   maxThreadCount                  = 4;
  26. SDL_Thread* thread    [maxThreadCount];
  27. int         threadTime[maxThreadCount];
  28. int         threadMiBs[maxThreadCount];
  29. char*       data;
  30.  
  31. struct ThreadMasterData {
  32.     SDL_atomic_t nextCacheLine;
  33.     SDL_atomic_t finalResult;
  34. } threadMasterData;
  35.  
  36. int workerThread(void* parameter)
  37. {
  38.     int count = 0;
  39.    
  40.     int futureData = SDL_AtomicAdd(&threadMasterData.nextCacheLine, cacheLineCountPerWorkThreadPull);
  41.    
  42.     while (1) {
  43.         int currentData = futureData;
  44.         int futureData  = SDL_AtomicAdd(&threadMasterData.nextCacheLine, cacheLineCountPerWorkThreadPull);
  45.        
  46.         for (int i = 0; i < 64 * cacheLineCountPerWorkThreadPull; ++i)
  47.             count += int(sqrt(data[currentData * 64 + i]));
  48.         if (futureData >= dataSizeCacheLines) break;
  49.     }
  50.    
  51.     SDL_AtomicAdd(&threadMasterData.finalResult, count);
  52.     return 0;
  53. }
  54.  
  55. int main(int argc, char* argv[])
  56. {
  57.     if (SDL_Init(SDL_INIT_TIMER) != 0) {
  58.         cout << "Unable to initialize SDL: " << SDL_GetError() << endl;
  59.         return -1;
  60.     }
  61.    
  62.     //srand(SDL_GetTicks());
  63.     srand(13);
  64.    
  65.     data = new char[dataSize];
  66.     cout << "seeding data...";
  67.     for (int i = 0; i < dataSize; ++i) data[i] = i % 256;
  68.     cout << "done" << endl;
  69.     cout << "Data size (MiB)   : " << dataSize / 1024 / 1024 << endl;
  70.     cout << "thread chunk cl   : " << cacheLineCountPerWorkThreadPull << endl;
  71.     cout << "thread chunk count: " << dataSizeCacheLines / cacheLineCountPerWorkThreadPull << endl;
  72.     for (int threadCount = 1; threadCount <= maxThreadCount; ++threadCount) {
  73.         SDL_AtomicSet(&threadMasterData.nextCacheLine, 0);
  74.         SDL_AtomicSet(&threadMasterData.finalResult, 0);
  75.        
  76.         cout << " starting " << threadCount << " threads... " << flush;
  77.         timerStart();
  78.         for (int i = 0; i < threadCount; ++i)
  79.             thread[i] = SDL_CreateThread(workerThread, "thread", 0);
  80.         for (int i = 0; i < threadCount; ++i) {
  81.             int threadReturnValue;
  82.             SDL_WaitThread(thread[i], &threadReturnValue);
  83.         }
  84.         Uint32 timeMs = timerEnd();
  85.         printTime(timeMs);
  86.         threadTime[threadCount - 1] = timeMs;
  87.         cout << " seconds (" << threadTime[threadCount - 1] * 100 / threadTime[0] << "%)" << endl;
  88.         cout << " result        : " << SDL_AtomicGet(&threadMasterData.finalResult) << endl;
  89.        
  90.         threadMiBs[threadCount - 1] = dataSize / timeMs;
  91.         cout << " MiB/Sec.      : " << dataSize / timeMs / 1000 << " (" << threadMiBs[threadCount - 1] * 100 / threadMiBs[0] << "%)" << endl;
  92.     }
  93.    
  94.     delete[] data;
  95.    
  96.     SDL_Quit();
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement