Parallel processing 重写一个简单的C++;将代码片段转换为CUDA代码 我编写了以下简单的C++代码。

Parallel processing 重写一个简单的C++;将代码片段转换为CUDA代码 我编写了以下简单的C++代码。,parallel-processing,cuda,Parallel Processing,Cuda,#include <iostream> #include <omp.h> int main() { int myNumber = 0; int numOfHits = 0; cout << "Enter my Number Value" << endl; cin >> myNumber; #pragma omp parallel for reduction(+:numOfHits)

#include <iostream>
#include <omp.h>

int main()
{
    int myNumber = 0;
    int numOfHits = 0;

    cout << "Enter my Number Value" << endl;
    cin >> myNumber;

    #pragma omp parallel for reduction(+:numOfHits)

    for(int i = 0; i <= 100000; ++i)
    {
        for(int j = 0; j <= 100000; ++j)
        {
            for(int k = 0; k <= 100000; ++k)
            {
                if(i + j + k == myNumber)
                    numOfHits++;
            }
        }
    }

    cout << "Number of Hits" << numOfHits << endl;

    return 0;
}
#包括
#包括
int main()
{
int myNumber=0;
int numOfHits=0;
cout-myNumber;
#pragma-omp-parallel用于缩减(+:numOfHits)

对于(inti=0;i好吧,我可以给你一个快速的教程,但我不一定要为你写全部

因此,首先,您需要使用CUDA设置MS Visual Studio,这很容易遵循以下指南:

<>现在你将要阅读英伟达CUDA编程指南(免费PDF)、文档和CUDA(我非常推荐学习CUDA)。

但假设你还没有这样做,以后肯定会这样做

这是一个计算量非常大、数据量非常小的计算-实际上,它可以在没有蛮力方法的情况下非常简单地进行计算,但这不是您要寻找的答案。我建议内核采用类似的方式:

__global__ void kernel(int* myNumber, int* numOfHits){

    //a shared value will be stored on-chip, which is beneficial since this is written to multiple times
    //it is shared by all threads
    __shared__ int s_hits = 0;

    //this identifies the current thread uniquely
    int i = (threadIdx.x + blockIdx.x*blockDim.x);
    int j = (threadIdx.y + blockIdx.y*blockDim.y);
    int k = 0;

    //we increment i and j by an amount equal to the number of threads in one dimension of the block, 16 usually, times the number of blocks in one dimension, which can be quite large (but not 100,000)
    for(; i < 100000; i += blockDim.x*gridDim.x){
        for(; j < 100000; j += blockDim.y*gridDim.y){
                  //Thanks to talonmies for this simplification
               if(0 <= (*myNumber-i-j) && (*myNumber-i-j) < 100000){
                  //you should actually use atomics for this
                 //otherwise, the value may change during the 'read, modify, write' process
                  s_hits++;
               }
        }
    }

    //synchronize threads, so we now s_hits is completely updated
    __syncthreads();

    //again, atomics
    //we make sure only one thread per threadblock actually adds in s_hits
    if(threadIdx.x == 0 && threadIdx.y == 0)
        *numOfHits += s_hits;

    return;
}
\uuuuu全局\uuuuuu无效内核(int*myNumber,int*numOfHits){
//共享值将存储在芯片上,这是有益的,因为它被多次写入
//它由所有线程共享
__共享\uuuu int s\u hits=0;
//这将唯一地标识当前线程
inti=(threadIdx.x+blockIdx.x*blockDim.x);
int j=(threadIdx.y+blockIdx.y*blockDim.y);
int k=0;
//我们将i和j的增量等于块的一维中的线程数,通常为16乘以一维中的块数,这可能相当大(但不是100000)
对于(;i<100000;i+=blockDim.x*gridDim.x){
对于(;j<100000;j+=blockDim.y*gridDim.y){
//感谢Talonmes的简化

如果(0),你也可以直接计算
numOfHits
,而不是用所有那些循环来强制执行它……你还没有问过一个问题。你到底想知道什么?内核中的内部循环完全没有必要。我相信只有当你启动足够多的线程来覆盖I和j域[099999]时,它才没有必要完全。否则,您将需要“逐步”覆盖没有专用线程的i和j。不。定义了i和j后,只有一个可能的值满足
i+j+k=myNumber
。因此,
k=myNumber-i-j
,因此您最多只能得到一个“命中”对于
k
的所有可能值,并且只有当
0时,代码才是不正确的,因为
s_hits++
被所有线程创建了一个争用条件。但是总体结构可能回答了一般问题,因为原始代码无论如何都有缺陷,如注释中所述。
dim3 blocks(some_number, some_number, 1); //some_number should be hand-optimized
dim3 threads(16, 16, 1);
kernel<<<blocks, threads>>>(/*args*/);