Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CUDA:在100x100矩阵中查找最小值_C_Cuda - Fatal编程技术网

CUDA:在100x100矩阵中查找最小值

CUDA:在100x100矩阵中查找最小值,c,cuda,C,Cuda,我刚刚学习了GPU编程,现在我有一个任务,通过在CUDA并行处理,从100x100矩阵中找到一个最小值。我尝试过这个代码,但它没有显示答案,而是显示我的初始值hmin=9999999。有人能给我正确的代码吗?哦,代码是C语言的 #include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> #define size (100*100) //Kernel Func

我刚刚学习了GPU编程,现在我有一个任务,通过在CUDA并行处理,从100x100矩阵中找到一个最小值。我尝试过这个代码,但它没有显示答案,而是显示我的初始值hmin=9999999。有人能给我正确的代码吗?哦,代码是C语言的

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define size (100*100)

//Kernel Functions & Variable
__global__ void FindMin(int* mat[100][100],int* kmin){
    int b=blockIdx.x+threadIdx.x*blockDim.x;
    int k=blockIdx.y+threadIdx.y*blockDim.y;

    if(mat[b][k] < kmin){
       kmin = mat[b][k];
    }

}

int main(int argc, char *argv[]) {
    //Declare Variabel
    int i,j,hmaks=0,hmin=9999999,hsumin,hsumax; //Host Variable
    int *da[100][100],*dmin,*dmaks,*dsumin,*dsumax; // Device Variable
    FILE *baca; //for opening txt file
    char buf[4]; //used for fscanf
    int ha[100][100],b; //matrix shall be filled by "b"

    //1: Read txt File
    baca=fopen("MatrixTubes1.txt","r");
    if (!baca){
       printf("Hey, it's not even exist"); //Checking File, is it there?
    }

    i=0;j=0; //Matrix index initialization
    if(!feof(baca)){ //if not end of file then do
        for(i = 0; i < 100; i++){
           for(j = 0; j < 100; j++){
              fscanf(baca,"%s",buf); //read max 4 char
              b=atoi(buf); //parsing from string to integer
              ha[i][j]=b; //save it to my matrix
           }
        }
    }
    fclose(baca);
    //all file has been read
    //time to close the file

    //Sesi 2: Allocation data di GPU
    cudaMalloc((void **)&da, size*sizeof(int));
    cudaMalloc((void **)&dmin, sizeof(int));
    cudaMalloc((void **)&dmaks, sizeof(int));
    cudaMalloc((void **)&dsumin, sizeof(int));
    cudaMalloc((void **)&dsumax, sizeof(int));

    //Sesi 3: Copy data to Device
    cudaMemcpy(da, &ha, size*sizeof(int), cudaMemcpyHostToDevice);
    cudaMemcpy(dmin, &hmin, sizeof(int), cudaMemcpyHostToDevice);
    cudaMemcpy(dmaks, &hmaks, sizeof(int), cudaMemcpyHostToDevice);

    //Sesi 4: Call Kernel

    FindMin<<<100,100,1>>>(da,dmin);

    //5: Copy from Device to Host

    cudaMemcpy(&hmin, dmin, sizeof(int), cudaMemcpyDeviceToHost);

    //6: Print that value
    printf("Minimum Value = %i \n",hmin);

    system("pause"); return 0;
 }

我在你的代码中看到了一些问题

正如MayurK在评论中提到的,您的索引错误。 正如MayurK所说,您正在比较两个指针,而不是它们所指向的值。 内核调用代码要求使用100x100x1网格,每个块只包含一个线程。这在效率方面是非常糟糕的。此外,由于这个原因,您的b和k的范围仅为0到99,因为threadIdx.x始终为零。 最后,所有线程都将并行运行,从而导致kmin=mat[b][k]中出现竞态条件,顺便说一句,该竞态条件应该是*kmin。修复索引问题后,同一块中的所有线程将同时写入全局内存中的位置。您应该使用atomicMin或并行约简来并行查找最小值。
如果FindMin中的*mat[b][k]<*kmin,不是吗?是的,我认为当所有变量都是int*时,这不是问题。但在这段代码发布之前我已经试过了。它不起作用,这是个问题。可能还有其他问题。在ifmat[b][k] Minimum Value = 9999999 Press any key to continue . . .