c - OpenMP giving inconsistent order of data -
i wrote code in c using openmp, first 4 results when compiling in different order , run time shows value other 0 half of time. missing something? here code:
#include <stdio.h> #include <stdlib.h> #include <omp.h> void test(int *ptr0, int *ptr1, int n) { int sum=0, i; int id=omp_get_thread_num(); for(i=0;i<16;i++){ sum+=prt0[i]*ptr1[i]; } printf("sum = %d thread %d\n",sum,id); } int main() { int m=64, n=16, a, b; int** array0=calloc(m, sizeof(int*)); for(a=0;a<m;a++){ array0[a]=calloc(n, sizeof(int)); } int** array1=calloc(m, sizeof(int*)); for(b=0;b<m;b++){ array1[b]=calloc(n, sizeof(int)); } for(i=0;i<16;i++);{ array0[i][n/2]=i; array1[i][n/2]=i; } #pragma omp parallel schedule(dynamic) for(i=0;i<n;i++){ test(array0[i],array1[i],n); } double start_time, run_time; start_time=omp_get_wtime(); run_time=omp_get_wtime()-start_time; printf("total run time = %.5g seconds\n", run_time); } the results (with different variations of first 4 lines):
sum = 0 thread 3 sum = 1 thread 1 sum = 4 thread 2 sum = 9 thread 3 sum = 16 thread 0 sum = 25 thread 1 sum = 36 thread 2 sum = 49 thread 3 sum = 64 thread 0 sum = 81 thread 1 sum = 100 thread 2 sum = 121 thread 3 sum = 144 thread 0 sum = 169 thread 1 sum = 196 thread 2 sum = 225 thread 3 total run time = 3.95e-07 seconds any suggestions on how can make results consistent sums being 0, 1, 4, 9, etc. , run time isn't 0 seconds?
why expect long duration requesting current start & end time in row? didn't want measure something? guess real target start time before loop starts , end time after tests done.
the order of outputs can't control because out of control how fast threads work. put results in additional array , print contents in main thread.
Comments
Post a Comment