0

私のCUDA Cの理解から、各スレッドは方程式のインスタンスを実行します。しかし、どうすればすべての値全体を出力できますか。コードは実際に機能しますが、誰かがレビューしてくれる必要があり、私の結果が実際に私が設計したものと一致していることを確認してください。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <conio.h>
#include <cuda.h>
#include <cutil.h>

__global__ void my_compute(float *y_d,float *theta_d,float *u_d)
{
    int idx=threadIdx.x+blockIdx.x*gridDim.x;

    for (idx=7;idx<1000;idx++)
    {
        y_d[idx]=theta_d[0]*y_d[idx-1]+theta_d[1]*y_d[idx-3]+
            theta_d[2]*u_d[idx-5]*u_d[idx-4]+theta_d[3]+
            theta_d[4]*u_d[idx-6]+theta_d[5]*u_d[idx-4]*y_d[idx-6]+
            theta_d[6]*u_d[idx-7]+theta_d[7]*u_d[idx-7]*u_d[idx-6]+
            theta_d[8]*y_d[idx-4]+theta_d[9]*y_d[idx-5]+
            theta_d[10]*u_d[idx-4]*y_d[idx-5]+theta_d[11]*u_d[idx-4]*y_d[idx-2]+
            theta_d[12]*u_d[idx-7]*u_d[idx-3]+theta_d[13]*u_d[idx-5]+
            theta_d[14]*u_d[idx-4];
    }
}

int main(void)
{   
    float y[1000];
    FILE *fpoo;
    FILE *u;
    float theta[15];
    float u_data[1000];
    float *y_d;
    float *theta_d;
    float *u_d;

    cudaEvent_t start,stop;
    float time;
    cudaEventCreate(&start);
    cudaEventCreate(&stop);

    // memory allocation

    cudaMalloc((void**)&y_d,1000*sizeof(float));
    cudaMalloc((void**)&theta_d,15*sizeof(float));
    cudaMalloc((void**)&u_d,1000*sizeof(float));
    cudaEventRecord( start, 0 );

    // importing data for theta and input of model//

    fpoo= fopen("c:\\Fly_theta.txt","r");
    u= fopen("c:\\Fly_u.txt","r");

    for (int k=0;k<15;k++)
    {
        fscanf(fpoo,"%f\n",&theta[k]);
    }
    for (int k=0;k<1000;k++)
    {
        fscanf(u,"%f\n",&u_data[k]);
    }

    //NB: pls does this for loop below make my equation run 1000000
    // instances as oppose to the 1000  instances i desire?
    for (int i=0;i<1000;i++)  
    {
        //i initialised the first 7 values of y because the equation output
        //starts form y(8)

        for (int k=0;k<8;k++)
        {
            y[k]=0;

            cudaMemcpy(y_d,y,1000*sizeof(float),cudaMemcpyHostToDevice);
            cudaMemcpy(theta_d,theta,15*sizeof(float),cudaMemcpyHostToDevice);
            cudaMemcpy(u_d,u_data,1000*sizeof(float),cudaMemcpyHostToDevice);

            //calling kernel function//
            my_compute<<<200,5>>>(y_d,theta_d,u_d);
            cudaMemcpy(y,y_d,1000*sizeof(float),cudaMemcpyDeviceToHost);
        }
        printf("\n\n*******Iteration %i*******\n", i);
        //does this actually print all the values from the threads? 

        for(int i=0;i<1000;i++)
        {
            printf("%f",y[i]);
        }
    }
    cudaEventRecord( stop, 0 );
    cudaEventSynchronize( stop );
    cudaEventElapsedTime( &time, start, stop );

    cudaEventDestroy( start );
    cudaEventDestroy( stop );
    printf("Time to generate:  %3.1f ms \n", time);

    cudaFree(y_d);
    cudaFree(theta_d);
    cudaFree(u_d);
    fclose(u);
    fclose(fpoo);
    //fclose();
    _getche();

    return (0);

}
4

1 に答える 1

1

すべての値全体を出力するにはどうすればよいですか。

さて、それをホストにコピーして(すでに行っています)、通常どおりに印刷できますか?

ただし、いくつかの理由であなたのコードが心配です。

  • 同じワープに属するスレッドだけが真に並列に実行されます。ワープは、32 個の隣接するスレッドの集まりです。(のようなものwarpId = threadIdx.x/32)。同期関数を適用しない限り、異なるワープに属するスレッドは任意の順序で実行できます。

  • y_d[idx-1]上記の理由により、を計算するときについて多くを語ることはできませんy_d[idx]y_d[idx-1]すでに他のスレッドによって計算/上書きされているかどうか?

  • ブロックには 5 つのスレッド (<<<200,5>>>) しかありませんが、ブロックはワープの粒度 (32 の倍数) で起動できるため、ブロックごとに 5 つのスレッドが実行され、27 のスレッドがアイドル状態のままになります。発売。

  • 並列処理をまったく使用していません! for1000 スレッドすべてで実行されるループがあります。1000 個のスレッドすべてがまったく同じことを計算します (競合状態を法として)。スレッド インデックスを計算しますidxが、それを完全に無視し、idxすべてのスレッドに対して 7 に設定します。

--- 起動構成、同期、スレッド インデックス作成の演習として --- 並列プレフィックスサムアルゴリズムを実装し、それが正しく機能することを確認した後にのみ、このより高度な処理を行うことを強くお勧めします...

于 2012-08-13T10:45:57.123 に答える