Cuda 我的代码是否运行非线性递归方程1000次迭代的1000个实例?

Cuda 我的代码是否运行非线性递归方程1000次迭代的1000个实例?,cuda,Cuda,根据我对CUDA C的理解,每个线程执行一个等式实例。但是我如何打印出所有的值呢。该代码实际上是工作,但真的需要有人审查它为我请确认我的结果实际上是内联的,我开始设计 #include <stdlib.h> #include <stdio.h> #include <string.h> #include <math.h> #include <conio.h> #include <cuda.h> #include <cut

根据我对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);

}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
__全局\uuuu无效我的\u计算(float*y\u d,float*theta\u d,float*u\d)
{
int idx=threadIdx.x+blockIdx.x*gridDim.x;
对于(idx=7;idx
如何打印出所有的值

那么,您可以将它复制到主机(您已经这样做了)并正常打印出来吗

但是,出于以下几个原因,我担心您的代码:

  • 只有属于同一扭曲的线程才能真正并行执行。扭曲是32个相邻线程的集合。(类似于
    warpId=threadIdx.x/32
    )。属于不同扭曲的线程可以以任何顺序执行,除非应用某些同步功能

  • 由于上述原因,在计算
    y\u d[idx]
    时,您不能对
    y\u d[idx-1]
    说太多。另一个线程是否已经计算/覆盖了
    y\u d[idx-1]

  • 块()中只有5个线程,但由于块可以以扭曲粒度(32的倍数)启动,因此每次启动块时,只需保持5个线程运行,27个线程空闲

  • 您根本没有使用并行性!您有一个
    for
    循环,它将由所有1000个线程执行。所有1000个线程计算完全相同的内容(按竞争条件进行模化)。您计算线程索引
    idx
    ,但完全忽略它,并将所有线程的
    idx
    设置为7


我强烈建议——作为启动配置、同步、线程索引的练习——实现一个并行算法,并且只有在确认它正常工作后,再做一些更高级的事情……

您可能需要注意正确格式化代码。特别是缩进。如果您看一下现在,你会注意到它看起来不对劲。谢谢你的观察。y[idx-1]被初始化为0,由于所有过去的输入都被设置为0,for循环从7开始。如果可以的话,我将取消for并创建块。我实际上是cuda c的新手。我掌握所有这些内容并完成cuda c上的项目工作的时间有限,因此所有这些问题。很多人感谢我理解有限的时间,但我没有我想你最好先尝试编写简单的代码。尽管语言相似,但在CUDA中编写并行代码需要与单线程C代码不同的思维方式。在你的情况下,循环从7开始、从0开始还是从42开始并不重要。使用循环索引而不是线程索引的事实违背了目的当然,你可以在代码中有一些循环,但是你应该让你的线程在某一点上做不同的事情…请你重写你所引用的代码的方面可以吗。所以我100%清楚。谢谢。首先,我不是100%确定你想做什么。其次,如果你想迭代一个1000000 ti公式mes和下一步直接取决于之前的值,您必须按顺序执行。CUDA并行性不会帮助您完成这项工作。但是,如果迭代步骤涉及一些独立的计算,这会有所帮助。第三,我担心大多数代码都必须重写;但我不想从根本上解决完整的问题ginning直到结束。好的,我会重写代码,如果可以的话,再给你看一次。我实际上是在寻找1000个类似方程的1000次迭代,因此这段代码是一个引导。一旦我得到了这个排序,下一阶段将使所有的1000个并行都有完全不同的输入。