kwat0308/gtracr

Feature: Add CPU parallelization for trajectory evaluation

Opened this issue · 1 comments

Implementation of some sort of parallelization will be nice for now. We can do this by using pools in Python.

For CPU parallelism in C++, OpenMP has a very convenient API. Here's a monte carlo approximation of PI with openMP enabled:

#include “omp.h”
static long num_trials = 10000;

int main (){
long i;      
long Ncirc = 0;       
double pi, x, y;
double r = 1.0;   // radius of circle. Side of squrare is 2*r seed(0,-r, r);  

// The circle and square are centered at the origin

#pragma omp parallel for private (x, y) reduction (+:Ncirc)
for(i=0;i<num_trials; i++){
    x = random();         
    y = random();
    if ( x*x + y*y) <= r*r)
        Ncirc++;
    }
    pi = 4.0 * ((double)Ncirc/(double)num_trials);
    printf("\n %d trials, pi is %f \n",num_trials, pi);
}

https://www.openmp.org/wp-content/uploads/omp-hands-on-SC08.pdf