零拷贝内存 or 页锁定内存

2018-01-09 11:01:25 浏览数 (1)

这是一个小实验,在于验证GPU上使用零拷贝内存和页锁定内存的性能差别。使用的是点积计算,数据量在100M左右。实验步骤很简单,分别在主机上开辟普通内存,页锁定内存以及进行零拷贝内存的操作,看三者哪个完成的时间比较快,具体的代码在最后,这里是实验结果:

但是,页锁定内存相比于零拷贝内存到底慢在哪里呢,当然是慢在从主机内存拷贝到显存的时间了,注释掉页锁定的拷贝语句之后,可以得到以下结果:

可以看出来,由于没有拷贝内存,得出的结果是错误的,但是时间确实别零拷贝内存少了。 ps:但是,奇怪的是,如果只将a,b内存拷贝的语句注释掉,页锁定内存仍旧可以得到正确的结果,暂时想不明白是为什么 这时就要问了,看起来零拷贝比页锁定要快啊,那还要这个页锁定干嘛呢,当然是有用的,因为,零拷贝的内容不能缓存在显存里,如果内容要被反复使用,零拷贝就要不停地从内存里取值,增加总线压力,这样相比页锁定也就处于劣势。至于多大的数据量和使用率才能使零拷贝的效率低于页锁定呢,这等以后再做实验来验证吧~

附代码:

代码语言:javascript复制
#include<stdio.h>

#define imin(a,b) (a<b?a:b)

const int N = 100 * 1024 * 1024;
const int threadsPerBlock = 256;
const int blocksPerGrid = imin(32, (N   threadsPerBlock - 1) / threadsPerBlock);

__global__ void dot(int size, float *a, float *b, float *c) {
    __shared__ float cache[threadsPerBlock];
    int tid = threadIdx.x   blockIdx.x * blockDim.x;
    int cacheIndex = threadIdx.x;

    float temp = 0;
    while (tid < size) {
        temp  = a[tid] * b[tid];
        tid  = blockDim.x * gridDim.x;
    }

    // set the cache values
    cache[cacheIndex] = temp;

    // synchronize threads in this block
    __syncthreads();

    // for reductions, threadsPerBlock must be a power of 2
    // because of the following code
    int i = blockDim.x / 2;
    while (i != 0) {
        if (cacheIndex < i)
            cache[cacheIndex]  = cache[cacheIndex   i];
        __syncthreads();
        i /= 2;
    }

    if (cacheIndex == 0)
        c[blockIdx.x] = cache[0];
}

float malloc_test(int size) {
    cudaEvent_t start, stop;
    float *a, *b, c, *partial_c;
    float *dev_a, *dev_b, *dev_partial_c;
    float elapsedTime;

    cudaEventCreate(&start);
    cudaEventCreate(&stop);

    // allocate memory on the CPU side
    a = (float*) malloc(size * sizeof(float));
    b = (float*) malloc(size * sizeof(float));
    partial_c = (float*) malloc(blocksPerGrid * sizeof(float));

    // allocate the memory on the GPU
    cudaMalloc((void**) &dev_a, size * sizeof(float));
    cudaMalloc((void**) &dev_b, size * sizeof(float));
    cudaMalloc((void**) &dev_partial_c, blocksPerGrid * sizeof(float));

    // fill in the host memory with data
    for (int i = 0; i < size; i  ) {
        a[i] = i;
        b[i] = i * 2;
    }

    cudaEventRecord(start, 0);
    // copy the arrays 'a' and 'b' to the GPU
    cudaMemcpy(dev_a, a, size * sizeof(float), cudaMemcpyHostToDevice);
    cudaMemcpy(dev_b, b, size * sizeof(float), cudaMemcpyHostToDevice);

    dot<<<blocksPerGrid, threadsPerBlock>>>(size, dev_a, dev_b, dev_partial_c);
    // copy the array 'c' back from the GPU to the CPU
    cudaMemcpy(partial_c, dev_partial_c, blocksPerGrid * sizeof(float),
            cudaMemcpyDeviceToHost);

    cudaEventRecord(stop, 0);
    cudaEventSynchronize(stop);
    cudaEventElapsedTime(&elapsedTime, start, stop);

    // finish up on the CPU side
    c = 0;
    for (int i = 0; i < blocksPerGrid; i  ) {
        c  = partial_c[i];
    }

    cudaFree(dev_a);
    cudaFree(dev_b);
    cudaFree(dev_partial_c);

    // free memory on the CPU side
    free(a);
    free(b);
    free(partial_c);

    // free events
    cudaEventDestroy(start);
    cudaEventDestroy(stop);

    printf("计算结果:  %fn", c);

    return elapsedTime;
}

float cuda_pinned_alloc_test(int size) {
    cudaEvent_t start, stop;
    float *a, *b, c, *partial_c;
    float *dev_a, *dev_b, *dev_partial_c;
    float elapsedTime;

    cudaEventCreate(&start);
    cudaEventCreate(&stop);

    // allocate the memory on the CPU
    cudaHostAlloc((void**) &a, size * sizeof(float),
            cudaHostAllocWriteCombined | cudaHostAllocMapped);
    cudaHostAlloc((void**) &b, size * sizeof(float),
            cudaHostAllocWriteCombined | cudaHostAllocMapped);
    cudaHostAlloc((void**) &partial_c, blocksPerGrid * sizeof(float),
            cudaHostAllocMapped);

    // find out the GPU pointers
    cudaHostGetDevicePointer(&dev_a, a, 0);
    cudaHostGetDevicePointer(&dev_b, b, 0);
    cudaHostGetDevicePointer(&dev_partial_c, partial_c, 0);

    // fill in the host memory with data
    for (int i = 0; i < size; i  ) {
        a[i] = i;
        b[i] = i * 2;
    }

    cudaEventRecord(start, 0);

    dot<<<blocksPerGrid, threadsPerBlock>>>(size, dev_a, dev_b, dev_partial_c);

    cudaThreadSynchronize();
    cudaEventRecord(stop, 0);
    cudaEventSynchronize(stop);
    cudaEventElapsedTime(&elapsedTime, start, stop);

    // finish up on the CPU side
    c = 0;
    for (int i = 0; i < blocksPerGrid; i  ) {
        c  = partial_c[i];
    }

    cudaFreeHost(a);
    cudaFreeHost(b);
    cudaFreeHost(partial_c);

    // free events
    cudaEventDestroy(start);
    cudaEventDestroy(stop);

    printf("计算结果:  %fn", c);

    return elapsedTime;
}

float cuda_host_alloc_test(int size) {
    cudaEvent_t start, stop;
    float *aa, *bb, c, *partial_cc;
    float *dev_aa, *dev_bb, *dev_partial_cc;
    float elapsedTime;

    cudaEventCreate(&start);
    cudaEventCreate(&stop);

    // allocate memory on the CPU side
    cudaHostAlloc((void**) &aa, size * sizeof(*aa), cudaHostAllocDefault);
    cudaHostAlloc((void**) &bb, size * sizeof(*bb), cudaHostAllocDefault);
    cudaHostAlloc((void**) &partial_cc, size * sizeof(*partial_cc), cudaHostAllocDefault);

    // allocate the memory on the GPU
    cudaMalloc((void**) &dev_aa, size * sizeof(float));
    cudaMalloc((void**) &dev_bb, size * sizeof(float));
    cudaMalloc((void**) &dev_partial_cc, blocksPerGrid * sizeof(float));

    // fill in the host memory with data
    for (int i = 0; i < size; i  ) {
        aa[i] = i;
        bb[i] = i * 2;
    }

    cudaEventRecord(start, 0);
    // copy the arrays 'a' and 'b' to the GPU

    cudaMemcpy(dev_aa, aa, size * sizeof(float), cudaMemcpyHostToDevice);
    cudaMemcpy(dev_bb, bb, size * sizeof(float), cudaMemcpyHostToDevice);


    dot<<<blocksPerGrid, threadsPerBlock>>>(size, dev_aa, dev_bb, dev_partial_cc);
    // copy the array 'c' back from the GPU to the CPU
/*  cudaMemcpy(partial_cc, dev_partial_cc, blocksPerGrid * sizeof(float),
            cudaMemcpyDeviceToHost);*/

    cudaEventRecord(stop, 0);
    cudaEventSynchronize(stop);
    cudaEventElapsedTime(&elapsedTime, start, stop);

    // finish up on the CPU side
    c = 0;
    for (int i = 0; i < blocksPerGrid; i  ) {
        c  = partial_cc[i];
    }

    cudaFree(dev_aa);
    cudaFree(dev_bb);
    cudaFree(dev_partial_cc);

    // free memory on the CPU side
    cudaFreeHost(aa);
    cudaFreeHost(bb);
    cudaFreeHost(partial_cc);

    // free events
    cudaEventDestroy(start);
    cudaEventDestroy(stop);

    printf("计算结果:  %fn", c);

    return elapsedTime;
}

int main(void) {
    cudaDeviceProp prop;
    int whichDevice;
    cudaGetDevice(&whichDevice);
    cudaGetDeviceProperties(&prop, whichDevice);
    if (prop.canMapHostMemory != 1) {
        printf("Device can not map memory.n");
        return 0;
    }
    float elapsedTime;

    cudaSetDeviceFlags (cudaDeviceMapHost);

    // try it with malloc
    elapsedTime = malloc_test(N);
    printf("普通主机内存:  %3.1f msn", elapsedTime);

    // now try it with cudaHostAlloc
    elapsedTime = cuda_pinned_alloc_test(N);
    printf("零拷贝内存:  %3.1f msn", elapsedTime);

    // now try it with cudaHostAlloc
    elapsedTime = cuda_host_alloc_test(N);
    printf("页锁定内存:  %3.1f msn", elapsedTime);
}

0 人点赞