File: array2d_raw.cu

package info (click to toggle)
python-escript 5.6-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 144,304 kB
  • sloc: python: 592,074; cpp: 136,909; ansic: 18,675; javascript: 9,411; xml: 3,384; sh: 738; makefile: 207
file content (51 lines) | stat: -rw-r--r-- 1,429 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <cusp/array2d.h>
#include <cusp/print.h>

// This example shows how to wrap a 2-dimensional array in
// "raw" memory on the device.  The matrix has dimensions 2x3
// and is stored in row major format.  Note that each row of the
// matrix has one padding element so the "pitch" is 4.
// 
//  Example Matrix (X is padding)
//   [ 1  2  3  X]
//   [ 4  5  6  X]


int main(void)
{
  // padding
  int X = -1;

  // 2d array in host memory
  int   host_A[8] = { 1, 2, 3, X, 4, 5, 6, X};

  // allocate device memory for 2d array
  int   * device_A;  cudaMalloc(&device_A, 8 * sizeof(int));

  // copy raw data from host to device
  cudaMemcpy(device_A, host_A, 8 * sizeof(int), cudaMemcpyHostToDevice);

  // *NOTE* raw pointers must be wrapped with thrust::device_ptr!
  thrust::device_ptr<int> wrapped_device_A(device_A);

  // use array1d_view to represent the linear array data
  typedef typename cusp::array1d_view< thrust::device_ptr<int> > DeviceArray1dView;

  DeviceArray1dView linear_array(wrapped_device_A, wrapped_device_A + 8);

  // use array2d_view to wrap the linear array
  typedef cusp::array2d_view<DeviceArray1dView, cusp::row_major> DeviceArray2dView;

  // construct a array2d_view to the 2x3 array with pitch=4
  DeviceArray2dView A(2, 3, 4, linear_array);

  // print the wrapped arrays
  cusp::print(linear_array);
  cusp::print(A);
    
  // free device arrays
  cudaFree(device_A);

  return 0;
}