Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL.h>
- #include <iostream>
- using namespace std;
- Uint32 tStart;
- void timerStart() {
- Uint32 c = SDL_GetTicks();
- while (c == SDL_GetTicks());
- tStart = SDL_GetTicks();
- }
- Uint32 timerEnd() {
- return SDL_GetTicks() - tStart;
- }
- void printTime(Uint32 timeMs) {
- cout << (static_cast<float>(timeMs) / 1000.0f);
- }
- const int dataSizeCacheLines = 8 * 1024 * 1024;
- const int dataSize = 64 * dataSizeCacheLines;
- const int cacheLineCountPerWorkThreadPull = 1024 / 64;
- const int maxThreadCount = 4;
- SDL_Thread* thread [maxThreadCount];
- int threadTime[maxThreadCount];
- int threadMiBs[maxThreadCount];
- char* data;
- struct ThreadMasterData {
- SDL_atomic_t nextCacheLine;
- SDL_atomic_t finalResult;
- } threadMasterData;
- int workerThread(void* parameter)
- {
- int count = 0;
- int futureData = SDL_AtomicAdd(&threadMasterData.nextCacheLine, cacheLineCountPerWorkThreadPull);
- while (1) {
- int currentData = futureData;
- int futureData = SDL_AtomicAdd(&threadMasterData.nextCacheLine, cacheLineCountPerWorkThreadPull);
- for (int i = 0; i < 64 * cacheLineCountPerWorkThreadPull; ++i)
- count += int(sqrt(data[currentData * 64 + i]));
- if (futureData >= dataSizeCacheLines) break;
- }
- SDL_AtomicAdd(&threadMasterData.finalResult, count);
- return 0;
- }
- int main(int argc, char* argv[])
- {
- if (SDL_Init(SDL_INIT_TIMER) != 0) {
- cout << "Unable to initialize SDL: " << SDL_GetError() << endl;
- return -1;
- }
- //srand(SDL_GetTicks());
- srand(13);
- data = new char[dataSize];
- cout << "seeding data...";
- for (int i = 0; i < dataSize; ++i) data[i] = i % 256;
- cout << "done" << endl;
- cout << "Data size (MiB) : " << dataSize / 1024 / 1024 << endl;
- cout << "thread chunk cl : " << cacheLineCountPerWorkThreadPull << endl;
- cout << "thread chunk count: " << dataSizeCacheLines / cacheLineCountPerWorkThreadPull << endl;
- for (int threadCount = 1; threadCount <= maxThreadCount; ++threadCount) {
- SDL_AtomicSet(&threadMasterData.nextCacheLine, 0);
- SDL_AtomicSet(&threadMasterData.finalResult, 0);
- cout << " starting " << threadCount << " threads... " << flush;
- timerStart();
- for (int i = 0; i < threadCount; ++i)
- thread[i] = SDL_CreateThread(workerThread, "thread", 0);
- for (int i = 0; i < threadCount; ++i) {
- int threadReturnValue;
- SDL_WaitThread(thread[i], &threadReturnValue);
- }
- Uint32 timeMs = timerEnd();
- printTime(timeMs);
- threadTime[threadCount - 1] = timeMs;
- cout << " seconds (" << threadTime[threadCount - 1] * 100 / threadTime[0] << "%)" << endl;
- cout << " result : " << SDL_AtomicGet(&threadMasterData.finalResult) << endl;
- threadMiBs[threadCount - 1] = dataSize / timeMs;
- cout << " MiB/Sec. : " << dataSize / timeMs / 1000 << " (" << threadMiBs[threadCount - 1] * 100 / threadMiBs[0] << "%)" << endl;
- }
- delete[] data;
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement