Vectorized data types and memory access

We will now look at CUDA's Vectorized Data Types. These are vectorized versions of the standard datatypes, such as int or double, in that they can store multiple values. There are vectorized versions of the 32-bit types of up to size 4 (for example, int2, int3, int4, and float4), while 64-bit variables can only be vectorized to be twice their original size (for example, double2 and long2). For a size 4 vectorized variable, we access each individual element using the C "struct" notation for the members x, y, z, and w, while we use x,y, and z for a 3-member variable and just x and y for a 2-member variable.

These may seem pointless right now, but these datatypes can be used to improve the performance of loading arrays from the global memory. Now, let's do a small test to see how we can load some int4 variables from an array of integers, and double2s from an array of doubles—we will have to use the CUDA reinterpret_cast operator to do this:

from __future__ import division
import numpy as np
from pycuda.compiler import SourceModule
import pycuda.autoinit
from pycuda import gpuarray

VecCode='''
__global__ void vec_ker(int *ints, double *doubles) {

int4 f1, f2;

f1 = *reinterpret_cast<int4*>(ints);
f2 = *reinterpret_cast<int4*>(&ints[4]);

printf("First int4: %d, %d, %d, %d\n", f1.x, f1.y, f1.z, f1.w);
printf("Second int4: %d, %d, %d, %d\n", f2.x, f2.y, f2.z, f2.w);

double2 d1, d2;

d1 = *reinterpret_cast<double2*>(doubles);
d2 = *reinterpret_cast<double2*>(&doubles[2]);

printf("First double2: %f, %f\n", d1.x, d1.y);
printf("Second double2: %f, %f\n", d2.x, d2.y);

}'''

Notice how we have to use the dereference operator * to set the vectorized variables, and how we have to jump to the next address by reference (&ints[4], &doubles[2]) to load the second int4 and double2 by using the reference operator & on the array:

This example is also available in the vectorized_memory.py file in this book's GitHub repository.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.221.149