将结构指针数组成员复制到另一个数组

时间:2014-08-26 01:51:58

标签: arrays pointers cuda structure memcpy

说,我有一个类似

的结构
struct vertex
{
  int x;
  int y;
  int z;
}

我制作了像

这样的指针数组
vertex *points = new vertex[100];

所以,在某一点上我只需要该指针数组的x个成员。那我怎么能这样做呢?

例如一个新指针:     int * xPoints = new int [100];

并在此我想存储顶点的所有x成员。有没有命令要这样做? 我不想使用循环,我想使用特殊的memcopy或其他东西。

1 个答案:

答案 0 :(得分:1)

cudaMemcpy2D可用于在源或目标(或两者)数据跨越时在主机和设备之间进行复制。

这是一个有效的例子:

$ cat t553.cu
#include <stdio.h>
#define DSIZE 4

struct vertex {
  int x,y,z;
};

__global__ void mykernel(int *data, unsigned length){
  for (int i = 0; i < length; i ++) printf("kernel data[%d] = %d\n",i,data[i]);
}

int main(){

  vertex *points = new vertex[DSIZE];
  for (int i = 0; i < DSIZE; i++){
    points[i].x = 1;
    points[i].y = 2;
    points[i].z = 3;}
  int *d_ypoints;
  cudaMalloc(&d_ypoints, DSIZE*sizeof(int));
  cudaMemcpy2D(d_ypoints, sizeof(int), ((int *)points)+1, 3*sizeof(int), sizeof(int), DSIZE, cudaMemcpyHostToDevice);
  mykernel<<<1,1>>>(d_ypoints, DSIZE);
  cudaDeviceSynchronize();
  return 0;
}


$ nvcc -arch=sm_20 -o t553 t553.cu
$ cuda-memcheck ./t553
========= CUDA-MEMCHECK
kernel data[0] = 2
kernel data[1] = 2
kernel data[2] = 2
kernel data[3] = 2
========= ERROR SUMMARY: 0 errors
$

解析cudaMemcpy2D操作:

  cudaMemcpy2D(d_ypoints,  // starting pointer on the device (destination)
             sizeof(int),  // stride on device (i.e. no stride)
       ((int *)points)+1,  // starting pointer on host (.y element of first struct)
           3*sizeof(int),  // stride on host (distance between consecutive .y elements)
             sizeof(int),  // number of bytes to transfer per "row"
                   DSIZE,  // number of "rows" to transfer
 cudaMemcpyHostToDevice);  // direction of transfer