Skip to content

Instantly share code, notes, and snippets.

@inequation
Created July 2, 2012 18:36

Revisions

  1. inequation revised this gist Jul 2, 2012. 1 changed file with 21 additions and 1 deletion.
    22 changes: 21 additions & 1 deletion gistfile1.cpp
    Original file line number Diff line number Diff line change
    @@ -65,6 +65,26 @@ void draw_parallel_for(int w, int h, const char *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;

    @@ -80,4 +100,4 @@ void draw_serial(int w, int h, const char *fname) {
    write_png(buf, w, h, fname);

    delete [] buf;
    }
    }
  2. inequation created this gist Jul 2, 2012.
    83 changes: 83 additions & 0 deletions gistfile1.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,83 @@
    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_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;
    }