float dist ( Point p1, Point p2, int dim )
{
int i;
float result=0.0;
for ( i=0; i<dim; i++ )
result += ( p1.coord[i] - p2.coord[i] ) * ( p1.coord[i] - p2.coord[i] );
return ( result );
}
I want to parallize this using threads ,but i keep getting errors.I am doing something like this
float dist(Point p1,Point p2,int dim){
int id;
int i,tnum,istart,iend;
id=omp_get_thread_num();
tnum=omp_get_num_threads();
istart=id*dim/tnum;
iend=(id+i)*dim/tnum;
float result=0.0;
#pragma omp parallel for reduction (+:result)
{ for ( i=istart; i<iend;i++ )
result += ( p1.coord[i] - p2.coord[i] ) * ( p1.coord[i] - p2.coord[i] );
return ( result );}
}
thank you
As far as dim
is not greater than (at least) 10^6, you will most probably won't see any speed gain using OpenMP.
The following code should work, but is not very useful for low values of DIM.
#include <iostream>
const int DIM = 3;
struct point {
float coord[DIM];
};
float dist(point p1, point p2, int dim)
{
float result = 0.0;
#pragma omp parallel for reduction(+:result)
for (int i=0; i<dim; i++ )
result += ( p1.coord[i] - p2.coord[i] ) * ( p1.coord[i] - p2.coord[i] );
return (result);
}
using namespace std;
int main()
{
point a = {0.0, 0.0, 0.0}, b = {2.0, 2.0, 2.0};
cout << dist(a, b, DIM) << endl;
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.