dimanche 1 mars 2015

OpenMp for dummies



I wrote this simple C code where I initialize a set of variables which are then copied them into a structure; this structure (which changes size every cycle of a for loop) is then passed to a function which cycle on its elements; my goal is to parallelize (using OpenMp) the code and I tried by adding the omp.h header and the this line: #pragma omp parallel for private(i)



#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <memory.h>
#include <string.h>
#include <omp.h>

typedef struct variables{
float Vx;
float Vy;
float Vz;
}velocity;

velocity *pv;

void func(velocity *pv,int id){
float V[3];

V[0]=1*pv[id].Vx;
V[1]=2*pv[id].Vy;
V[2]=3*pv[id].Vz;

pv[id].Vx=V[0];
pv[id].Vy=V[1];
pv[id].Vz=V[2];

}

int main(){
int j,jcoll,i,Np = 100, Nmax=2;
float Vx[Np],Vy[Np],Vz[Np];
srand(time(NULL));
float r; int q;

for(i=0;i<Np;i++){
r = rand()%10;
Vx[i]=1*r;
r = rand()%10;
Vy[i]=2*r;
r = rand()%10;
Vz[i]=3*r;
}

for(jcoll=1;jcoll<=Nmax;jcoll++){
pv = (velocity*)malloc(jcoll*sizeof(velocity));

for(j=0;j<jcoll;j++){
pv[j].Vx = Vx[j];
pv[j].Vy = Vy[j];
pv[j].Vz = Vz[j];
}

#pragma omp parallel for private(i) // Is this correct?
for(i=0;i<jcoll;i++){
func(pv,i);
}


free(pv);
}

return 0;
}


Can anyone check if what I wrote makes sense or should I try to do something else in order to do a correct parallelization?




Aucun commentaire:

Enregistrer un commentaire