Cuda 仅存储循环中迭代次数最少的值

Cuda 仅存储循环中迭代次数最少的值,cuda,global,commit,Cuda,Global,Commit,我有N个迭代和L个内存位置。每次迭代都会写入0到(L-1)个内存位置中的任意一个 我想根据迭代次数将数据存储在全局内存中。假设迭代K和K-1都写入相同的位置。全局内存中的最终结果应该是K。线程K-1不应该覆盖线程K的结果 我已经尝试了下面的解决方案。我为每个内存位置创建一个具有最大迭代次数的数组,并初始化为-1。然后检查迭代次数是否大于当前值。如果是,则存储该值并更新最大迭代次数 这是我当前的代码,但对于较大的迭代,它没有给出正确的结果 #include<stdio.h> #incl

我有N个迭代和L个内存位置。每次迭代都会写入0到(L-1)个内存位置中的任意一个

我想根据迭代次数将数据存储在全局内存中。假设迭代K和K-1都写入相同的位置。全局内存中的最终结果应该是K。线程K-1不应该覆盖线程K的结果

我已经尝试了下面的解决方案。我为每个内存位置创建一个具有最大迭代次数的数组,并初始化为-1。然后检查迭代次数是否大于当前值。如果是,则存储该值并更新最大迭代次数

这是我当前的代码,但对于较大的迭代,它没有给出正确的结果

#include<stdio.h>
#include"cuda.h"
__global__ void  fun(int *A,int *maxIndex,int *index1,int *lock)
{
                    int threadid=blockIdx.x*blockDim.x+threadIdx.x;
                    int iteration_no=threadid;
                    int index=index1[threadid];
                    int exitFromLoop=1;
                    while(exitFromLoop==1)
                   {
                            int flag=atomicCAS(&lock[index],0,1);
                     if(flag==0)


                           {
                                    if(maxIndex[index]<iteration_no)
                                    {       
                                            A[index]=89;
                                            maxIndex[index]=iteration_no;
                                             __threadfence();
                                    }
                                    else
                                    {
                                    }
                                    //__threadfence();
                                    lock[index]=0;
                                    exitFromLoop=0;
                            }
                            else
                            {

                            }
                    }
#包括
#包括“cuda.h”
__全局无效乐趣(int*A,int*maxIndex,int*index1,int*lock)
{
int-threadid=blockIdx.x*blockDim.x+threadIdx.x;
int迭代_no=threadid;
int index=index1[threadid];
int exitFromLoop=1;
while(exitFromLoop==1)
{
int flag=atomicCAS(&lock[index],0,1);
如果(标志==0)
{

如果(maxIndex[index]我想这可能就是你想要的

对于
A
数组中的每个元素,在
maxIndex
数组中有一个对应的元素,其中包含更新
A
数组中元素的最后一个线程的
iteration\u no
。如果当前线程的
iteration\u no
高于该值,则
maxIndex
将更新为<当前线程的code>iteration\u no
,线程更新
A
中的元素

如果当前线程的迭代次数较低,则
a
元素不会得到更新,而
maxIndex
中的
迭代次数也不会得到更新

#include<stdio.h>
#include"cuda.h"

__global__ void  fun(int *A,int *maxIndex,int *index)
{
  int iteration_no=blockIdx.x*blockDim.x+threadIdx.x;
  int i=index[iteration_no];
  if (atomicMax(maxIndex + i, iteration_no) < iteration_no) {
    A[i] = 89;
  }
}

int main()
{
  int A[10] = {10,20,30,40,50,60,70,80,90,100};
  int maxIndex[10]={-1};
  int index[8192];
  srand(0);
  for(int ii=0;ii<8192;ii++)
  {
    index[ii]=rand()%10;
  }
  int *index_d;
  int *A_d,*maxIndex_d;
  cudaMalloc((void**)&A_d,sizeof(int)*10);
  cudaMalloc((void**)&index_d,sizeof(int)*8192);
  cudaMalloc((void**)&maxIndex_d,sizeof(int)*10);
  cudaMemcpy(A_d,&A,sizeof(int)*10,cudaMemcpyHostToDevice);
  cudaMemcpy(maxIndex_d,&maxIndex,sizeof(int)*10,cudaMemcpyHostToDevice);
  cudaMemcpy(index_d,&index,sizeof(int)*8192,cudaMemcpyHostToDevice);

  fun<<<16,512>>>(A_d,maxIndex_d,index_d);

  cudaMemcpy(&A,A_d,sizeof(int)*10,cudaMemcpyDeviceToHost);
  cudaMemcpy(&maxIndex,maxIndex_d,sizeof(int)*10,cudaMemcpyDeviceToHost);
  printf("\nindex \n");

  for(int i=0;i<8192;i++) {
    printf("%d\n",index[i]);
  }

  for(int i=0;i<10;i++) {
    printf(" %d max is %d\n",A[i],maxIndex[i]);
  }
}                                                 
#包括
#包括“cuda.h”
__全局无效乐趣(int*A,int*maxIndex,int*index)
{
int iteration_no=blockIdx.x*blockDim.x+threadIdx.x;
int i=索引[迭代编号];
if(atomicMax(maxIndex+i,迭代编号)<迭代编号){
A[i]=89;
}
}
int main()
{
inta[10]={10,20,30,40,50,60,70,80,90100};
int-maxIndex[10]={-1};
整数指数[8192];
srand(0);

对于(int ii=0;ii你能修改问题文本吗?它既混乱又不清楚。如果人们理解你,他们肯定会帮助你。实际上我有n次迭代,我想将数据提交到全局内存中的一个数组中,提交应该是这样的假设第n次迭代和n-1都在写入第i个索引,那么最终结果将是所以保留第n个结果并覆盖由n-1迭代编写的结果。以循环和in-loop为例,在o/p中实际需要的最终结果是什么请回复某人。我需要help@user3279286,这个问题是否仍然正确地描述了你正在尝试做的事情?@user3279286,迭代_no来自哪里?你能解释一下吗这正是我想要的sirnow这段代码对任何线程、块和数组大小都是正确的,否则你会修改它。我尝试了一些,它给出了正确的结果。我做了一个小的修改,将maxIndex的起点从0改为-1,以确保线程0也能进行更新。非常感谢你,先生。我试着找出我的代码有什么问题。我知道假设maxIndex 999是由2个线程写入的,将同时读取该值假设1000和1089,那么如果线程1000稍后写入,那么最终结果将由1000写入,如果线程1089稍后写入,那么最终结果将由该线程写入,但实际上我们使用的是l那么为什么两个线程同时读取同一个maxIndex[k]的值会发生这种情况呢?
#include<stdio.h>
#include"cuda.h"

__global__ void  fun(int *A,int *maxIndex,int *index)
{
  int iteration_no=blockIdx.x*blockDim.x+threadIdx.x;
  int i=index[iteration_no];
  if (atomicMax(maxIndex + i, iteration_no) < iteration_no) {
    A[i] = 89;
  }
}

int main()
{
  int A[10] = {10,20,30,40,50,60,70,80,90,100};
  int maxIndex[10]={-1};
  int index[8192];
  srand(0);
  for(int ii=0;ii<8192;ii++)
  {
    index[ii]=rand()%10;
  }
  int *index_d;
  int *A_d,*maxIndex_d;
  cudaMalloc((void**)&A_d,sizeof(int)*10);
  cudaMalloc((void**)&index_d,sizeof(int)*8192);
  cudaMalloc((void**)&maxIndex_d,sizeof(int)*10);
  cudaMemcpy(A_d,&A,sizeof(int)*10,cudaMemcpyHostToDevice);
  cudaMemcpy(maxIndex_d,&maxIndex,sizeof(int)*10,cudaMemcpyHostToDevice);
  cudaMemcpy(index_d,&index,sizeof(int)*8192,cudaMemcpyHostToDevice);

  fun<<<16,512>>>(A_d,maxIndex_d,index_d);

  cudaMemcpy(&A,A_d,sizeof(int)*10,cudaMemcpyDeviceToHost);
  cudaMemcpy(&maxIndex,maxIndex_d,sizeof(int)*10,cudaMemcpyDeviceToHost);
  printf("\nindex \n");

  for(int i=0;i<8192;i++) {
    printf("%d\n",index[i]);
  }

  for(int i=0;i<10;i++) {
    printf(" %d max is %d\n",A[i],maxIndex[i]);
  }
}