Value in array

General OpenMP discussion

Value in array

Postby wearetherock » Sun Sep 07, 2008 10:01 am

Code: Select all
#include <stdio.h>
#include <omp.h>
#include <math.h>
#define SIZE 16

void printArray(int a[]){
    printf("[");

    for(int i=0; i< SIZE ;i++){
          printf("%d, ",a[i]);
    }
    printf("\b\b]\n");
}

void clearArray(int a[]){
   for(int i=0;i <SIZE ;i++){
      a[i] = 0;
   }
}
int main(int argc, char **argv){
   int m = log2(SIZE);
   int a[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
   printArray(a);
   int f[16];

   omp_set_num_threads(SIZE);

   for(int i=0; i<m ;i++){
      #pragma omp parallel
      {

         int k = pow(2,i);
         int tid = omp_get_thread_num();

         if(tid > (tid ^ k)){
            if(f[tid ^ k] = 0){
               a[tid] = a[tid]*2 + a[tid ^ k];
            }else{
               a[tid] = a[tid] + a[tid ^ k];
            }
            //printf("> %d %d\n",tid,tid ^k);
         }else{
            if(f[tid ^ k] = 0){
               a[tid] = a[tid] + a[tid ^ k];
            }else{
               a[tid] = a[tid] + (a[tid ^ k] - a[tid])/2;
            }
            //printf("  < %d %d\n",tid,tid ^k);      
         }

         f[tid] = 1;
         printf(" tid = %d f[%d] = %d\n", tid,tid,f[tid]);
         #pragma omp barrier
         if(tid ==5){
            printf("---------- i = %d ----------\n",i);
            printArray(a);
            printArray(f);
            clearArray(f);
         }
         #pragma omp barrier
      }

   }
   printArray(a);
   printArray(f);
}


What happen with Array f

Code: Select all
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
tid = 1 f[1] = 1
tid = 2 f[2] = 1
tid = 3 f[3] = 1
tid = 4 f[4] = 1
tid = 5 f[5] = 1
tid = 6 f[6] = 1
tid = 7 f[7] = 1
tid = 8 f[8] = 1
tid = 9 f[9] = 1
tid = 10 f[10] = 1
tid = 11 f[11] = 1
tid = 12 f[12] = 1
tid = 13 f[13] = 1
tid = 14 f[14] = 1
tid = 15 f[15] = 1
tid = 0 f[0] = 1
---------- i = 0 ----------
[0, 1, 2, 5, 4, 9, 6, 13, 8, 17, 10, 21, 12, 25, 14, 29]
[1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1] << Problem
tid = 11 f[11] = 1
tid = 12 f[12] = 1
tid = 7 f[7] = 1
tid = 8 f[8] = 1
tid = 10 f[10] = 1
tid = 9 f[9] = 1
tid = 13 f[13] = 1
tid = 14 f[14] = 1
tid = 4 f[4] = 1
tid = 6 f[6] = 1
tid = 3 f[3] = 1
tid = 2 f[2] = 1
tid = 1 f[1] = 1
tid = 5 f[5] = 1
tid = 15 f[15] = 1
tid = 0 f[0] = 1
---------- i = 1 ----------
[1, 3, 2, 6, 5, 15, 11, 22, 9, 27, 19, 38, 13, 27, 27, 56]
[1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1] << Problem
tid = 10 f[10] = 1
tid = 3 f[3] = 1
tid = 2 f[2] = 1
tid = 1 f[1] = 1
tid = 8 f[8] = 1
tid = 6 f[6] = 1
tid = 13 f[13] = 1
tid = 12 f[12] = 1
tid = 11 f[11] = 1
tid = 7 f[7] = 1
tid = 14 f[14] = 1
tid = 4 f[4] = 1
tid = 9 f[9] = 1
tid = 5 f[5] = 1
tid = 15 f[15] = 1
tid = 0 f[0] = 1
---------- i = 2 ----------
[3, 9, 6, 14, 6, 24, 17, 36, 11, 40, 23, 47, 24, 54, 50, 103]
[1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1] << Problem
tid = 11 f[11] = 1
tid = 6 f[6] = 1
tid = 12 f[12] = 1
tid = 3 f[3] = 1
tid = 13 f[13] = 1
tid = 8 f[8] = 1
tid = 1 f[1] = 1
tid = 10 f[10] = 1
tid = 2 f[2] = 1
tid = 4 f[4] = 1
tid = 14 f[14] = 1
tid = 7 f[7] = 1
tid = 9 f[9] = 1
tid = 5 f[5] = 1
tid = 15 f[15] = 1
tid = 0 f[0] = 1
---------- i = 3 ----------
[8, 24, 17, 37, 18, 51, 33, 69, 14, 64, 29, 61, 30, 78, 83, 172]
[1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1]  << Problem
[8, 24, 17, 37, 18, 51, 33, 69, 14, 64, 29, 61, 30, 78, 83, 172]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]



Why it not be
Code: Select all
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
wearetherock
 
Posts: 1
Joined: Sun Sep 07, 2008 9:12 am

Re: Value in array

Postby ejd » Sun Sep 07, 2008 2:35 pm

What compiler (vendor and version) and OS are you using?? Do you have any OpenMP environment variables set??
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: Value in array

Postby ejd » Thu Sep 11, 2008 8:59 am

I looked at this again and the problem is that you made a mistake in your C code:
Code: Select all
line 36     if (f[tid ^ k] = 0) {
should be:  if (f[tid ^ k] == 0) {

line 43     if (f[tid ^ k] = 0) {
should be:  if (f[tid ^ k] == 0) {
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am


Return to Using OpenMP

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 8 guests