Created
July 2, 2012 18:36
-
-
Save inequation/3034800 to your computer and use it in GitHub Desktop.
Drawer models
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
std::queue< std::pair<int, int> * > task_queue; | |
void draw_pooled(int w, int h, const char *fname) { | |
unsigned char *buf; | |
buf = new unsigned char[w * h * 3]; | |
Scene::GetInstance().PrepareRender(w, h); | |
bool tasks_issued = false; | |
#pragma omp parallel shared(buf, tasks_issued, w, h) | |
{ | |
#pragma omp master | |
{ | |
for (int y = 0; y < h; ++y) { | |
for (int x = 0; x < w; ++x) | |
task_queue.push(new std::pair<int, int>(x, y)); | |
} | |
tasks_issued = true; | |
} | |
while (true) { | |
std::pair<int, int> *coords; | |
#pragma omp critical(task_fetch) | |
{ | |
if (task_queue.size() > 0) { | |
coords = task_queue.front(); | |
task_queue.pop(); | |
} else | |
coords = NULL; | |
} | |
if (coords != NULL) { | |
Scene::GetInstance().RenderPixel(coords->first, coords->second, | |
buf + (coords->second * w + coords->first) * 3); | |
delete coords; | |
} else { | |
#pragma omp flush(tasks_issued) | |
if (tasks_issued) | |
break; | |
} | |
} | |
} | |
write_png(buf, w, h, fname); | |
delete [] buf; | |
} | |
void draw_parallel_for(int w, int h, const char *fname) { | |
unsigned char *buf; | |
buf = new unsigned char[w * h * 3]; | |
Scene::GetInstance().PrepareRender(w, h); | |
for (int y = 0; y < h; ++y) { | |
#pragma omp parallel for | |
for (int x = 0; x < w; ++x) | |
Scene::GetInstance().RenderPixel(x, y, buf + (y * w + x) * 3); | |
} | |
write_png(buf, w, h, fname); | |
delete [] buf; | |
} | |
void draw_parallel_for2(int w, int h, const char *fname) { | |
unsigned char *buf; | |
buf = new unsigned char[w * h * 3]; | |
Scene::GetInstance().PrepareRender(w, h); | |
int x, y; | |
#pragma omp parallel for private(x, y) | |
for (int xy = 0; xy < w * h; ++xy) { | |
x = xy % w; | |
y = xy / w; | |
Scene::GetInstance().RenderPixel(x, y, buf + (y * w + x) * 3); | |
} | |
write_png(buf, w, h, fname); | |
delete [] buf; | |
} | |
void draw_serial(int w, int h, const char *fname) { | |
unsigned char *buf; | |
buf = new unsigned char[w * h * 3]; | |
Scene::GetInstance().PrepareRender(w, h); | |
for (int y = 0; y < h; ++y) { | |
for (int x = 0; x < w; ++x) | |
Scene::GetInstance().RenderPixel(x, y, buf + (y * w + x) * 3); | |
} | |
write_png(buf, w, h, fname); | |
delete [] buf; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment