2011-08-13 16 views
2

私はGPGPUエミュレータをc & pthreadsで作ろうとしていましたが、なぜそれが起こっているのか分かりません。コードは以下の通りです:C pthreadセグメンテーションフォールト

#include <stdlib.h> 
#include <stdio.h> 
#include <pthread.h> 
#include <assert.h> 

// simplifies malloc 
#define MALLOC(a) (a *)malloc(sizeof(a)) 

// Index of x/y coordinate 
#define x (0) 
#define y (1) 

// Defines size of a block 
#define BLOCK_DIM_X (3) 
#define BLOCK_DIM_Y (2) 

// Defines size of the grid, i.e., how many blocks 
#define GRID_DIM_X (5) 
#define GRID_DIM_Y (7) 

// Defines the number of threads in the grid 
#define GRID_SIZE (BLOCK_DIM_X * BLOCK_DIM_Y * GRID_DIM_X * GRID_DIM_Y) 

// execution environment for the kernel 
typedef struct exec_env { 
    int threadIdx[2]; // thread location 
    int blockIdx[2]; 
    int blockDim[2]; 
    int gridDim[2]; 

    float *A,*B;  // parameters for the thread 
    float *C; 
} exec_env; 

// kernel 
void *kernel(void *arg) 
{ 
    exec_env *env = (exec_env *) arg; 

    // compute number of threads in a block 
    int sz = env->blockDim[x] * env->blockDim[y]; 

    // compute the index of the first thread in the block 
    int k = sz * (env->blockIdx[y]*env->gridDim[x] + env->blockIdx[x]); 

    // compute the index of a thread inside a block 
    k = k + env->threadIdx[y]*env->blockDim[x] + env->threadIdx[x]; 

    // check whether it is in range 
    assert(k >= 0 && k < GRID_SIZE && "Wrong index computation"); 

    // print coordinates in block and grid and computed index 
    /*printf("tx:%d ty:%d bx:%d by:%d idx:%d\n",env->threadIdx[x], 
               env->threadIdx[y], 
               env->blockIdx[x], 
               env->blockIdx[y], k); 
    */ 

    // retrieve two operands 
    float *A = &env->A[k]; 
    float *B = &env->B[k]; 
    printf("%f %f \n",*A, *B); 
    // retrieve pointer to result 
    float *C = &env->C[k]; 

    // do actual computation here !!! 

    // For assignment replace the following line with 
    // the code to do matrix addition and multiplication. 
    *C = *A + *B; 

    // free execution environment (not needed anymore) 
    free(env); 

    return NULL; 
} 


// main function 
int main(int argc, char **argv) 
{ 
    float A[GRID_SIZE] = {-1}; 
    float B[GRID_SIZE] = {-1}; 
    float C[GRID_SIZE] = {-1}; 

    pthread_t threads[GRID_SIZE]; 

    int i=0, bx, by, tx, ty; 
    //Error location 
    /*for (i = 0; i < GRID_SIZE;i++){ 
     A[i] = i; 
     B[i] = i+1; 
     printf("%f %f\n ", A[i], B[i]); 
    }*/ 

    // Step 1: create execution environment for threads and create thread 
    for (bx=0;bx<GRID_DIM_X;bx++) { 
     for (by=0;by<GRID_DIM_Y;by++) { 
     for (tx=0;tx<BLOCK_DIM_X;tx++) { 
      for (ty=0;ty<BLOCK_DIM_Y;ty++) { 

       exec_env *e = MALLOC(exec_env); 
       assert(e != NULL && "memory exhausted"); 

       e->threadIdx[x]=tx; 
       e->threadIdx[y]=ty; 

       e->blockIdx[x]=bx; 
       e->blockIdx[y]=by; 

       e->blockDim[x]=BLOCK_DIM_X; 
       e->blockDim[y]=BLOCK_DIM_Y; 

       e->gridDim[x]=GRID_DIM_X; 
       e->gridDim[y]=GRID_DIM_Y; 

       // set parameters 
       e->A = A; 
       e->B = B; 
       e->C = C; 

       // create thread 
       pthread_create(&threads[i++],NULL,kernel,(void *)e); 
      } 
     } 
     } 
    } 

    // Step 2: wait for completion of all threads 
    for (i=0;i<GRID_SIZE;i++) { 
     pthread_join(threads[i], NULL); 
    } 

    // Step 3: print result  
    for (i=0;i<GRID_SIZE;i++) { 
     printf("%f ",C[i]); 
    } 
    printf("\n"); 

    return 0; 
} 

[OK]を、このコードはここで罰金動作しますが、できるだけ早く私は、forループ( "エラーの場所" をアンコメントとして割り当てる[I] = iとB [i]は= I + 1、私はunixのセグメンテーション違反とcygwinのC内のこれらのランダムな0によってスナップされます。私はCで私のファンダメンタルズがかなり貧しいことを認めなければならないので、私は何かを逃した可能性が高い可能性があります。間違って何が起こっているのかにそれをいただければと思います。ありがとうございました。

答えて

5

あなたは4つのネストされたループが起動したときにiはまだ0であるため、というコメントとき、それは動作します。

あなた時間これをaveの:

for (i = 0; i < GRID_SIZE;i++){ 
    A[i] = i; 
    B[i] = i+1; 
    printf("%f %f\n ", A[i], B[i]); 
} 

/* What value is `i` now ? */ 

そして

pthread_create(&threads[i++],NULL,kernel,(void *)e); 
         ^

のでpthread_createは確かにいくつかの興味深いのインデックスにアクセスしようとします。

+0

私はfacepalmedです。おかげで多くのcnicutar! –

関連する問題