【问题标题】:Contiguous Memory Allocation on GPUGPU 上的连续内存分配
【发布时间】:2012-07-03 01:47:26
【问题描述】:

cudaMalloc 是否分配连续的内存块(即彼此相邻的物理字节)?

我有一段 CUDA 代码,它使用 32 个线程简单地将 128 个字节从全局设备内存复制到共享内存。我试图找到一种方法来保证可以在一个 128 个字节的内存事务中完成此传输。如果 cudaMalloc 分配的是连续的内存块,那么很容易做到。

以下是代码:

#include <iostream>

using namespace std;
#define SIZE 32 //SIZE of the array to store in shared memory                                                                                                                        
#define NUMTHREADS 32
__global__ void copy(uint* memPointer){

  extern __shared__ uint bits[];
  int tid = threadIdx.x;

  bits[tid] = memPointer[tid];

}

int main(){
  uint inputData[SIZE];
  uint* storedData;
  for(int  i=0;i<SIZE;i++){
    inputData[i] = i;
  }
  cudaError_t e1=cudaMalloc((void**) &storedData, sizeof(uint)*SIZE);
  if(e1 == cudaSuccess){
    cudaError_t e3= cudaMemcpy(storedData, inputData, sizeof(uint)*SIZE, cudaMemcpyHostToDevice);
      if(e3==cudaSuccess){
        copy<<<1,NUMTHREADS, SIZE*4>>>(storedData);
            cudaError_t e6 = cudaFree(storedData);
            if(e6==cudaSuccess){
            }
            else{
              cout << "Error freeing memory storedData" << e6 << endl;
            }
      }
      else{
        cout << "Failed to copy" << " " << e3 << endl;
      }

  }
  else{
    cout << "Failed to allocate memory" << " " << e1 << endl;

  }
  return 0;
}

【问题讨论】:

  • 内核应该服务于什么目的?
  • 它是我对数据执行一些操作的较大代码的一部分。我正在尝试优化代码的各个部分。
  • 如果 128 字节块是 128 字节对齐的,那么这将在一个事务中完成。 NVIDIA GPU 有一个独立于 CPU MMU 的 MMU。所有 GPU 内存操作都是通过 GPU 的虚拟地址空间完成的。不能保证大于缓存行的块在物理上是连续的。

标签: cuda gpu


【解决方案1】:

是的,cudaMalloc 分配连续的内存块。 SDK (http://developer.nvidia.com/cuda-cc-sdk-code-samples) 中的“Matrix Transpose”示例有一个名为“copySharedMem”的内核,它几乎完全符合您的描述。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 2011-07-12
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    相关资源
    最近更新 更多