【问题标题】:OpenCL pinned memory vs Heap MemoryOpenCL 固定内存与堆内存
【发布时间】:2016-02-10 13:55:51
【问题描述】:

我编写了一个示例程序来了解 GPU/CPU 固定内存和堆内存的影响。下面的代码说明了这一点。我分配了三个尺寸为 1280x720 的缓冲区。我已经用一些数据填充了缓冲区 1 和 2,然后使用这些缓冲区来填充缓冲区 3。填充缓冲区 3 所涉及的数学运算是微不足道的。在情况 1 中,从这些缓冲区分配的内存来自堆(malloc 调用)。在情况 2 中,这些缓冲区的内存是通过 OpenCL API 调用 (clCreateBuffer()) 分配的。这两种情况之间存在性能差异。我在英特尔集成 GPU 上对其进行了测试。我无法解释这种性能差异。它是否与 CPU/GPU 固定内存与堆内存的可缓存属性有关。

你之前有没有遇到过这样的行为,还是我做错了什么?

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>

#define OPENCL

#if defined(_WIN32)
/*
 * Win32 specific includes
 */
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#else
#include <sys/time.h>

/* timersub is not provided by msys at this time. */
#ifndef timersub
#define timersub(a, b, result) \
    do { \
      (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
      (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
      if ((result)->tv_usec < 0) { \
        --(result)->tv_sec; \
        (result)->tv_usec += 1000000; \
      } \
    } while (0)
#endif
#endif


struct usec_timer {
#if defined(_WIN32)
  LARGE_INTEGER  begin, end;
#else
  struct timeval begin, end;
#endif
};


static void usec_timer_start(struct usec_timer *t) {
#if defined(_WIN32)
  QueryPerformanceCounter(&t->begin);
#else
  gettimeofday(&t->begin, NULL);
#endif
}


static void usec_timer_mark(struct usec_timer *t) {
#if defined(_WIN32)
  QueryPerformanceCounter(&t->end);
#else
  gettimeofday(&t->end, NULL);
#endif
}


static int64_t usec_timer_elapsed(struct usec_timer *t) {
#if defined(_WIN32)
  LARGE_INTEGER freq, diff;

  diff.QuadPart = t->end.QuadPart - t->begin.QuadPart;

  QueryPerformanceFrequency(&freq);
  return diff.QuadPart * 1000000 / freq.QuadPart;
#else
  struct timeval diff;

  timersub(&t->end, &t->begin, &diff);
  return diff.tv_sec * 1000000 + diff.tv_usec;
#endif
}


#ifdef OPENCL
#include ".\CL\cl.h"

int opencl_init(cl_context *context, cl_command_queue *cmd_queue) {
  cl_int status;
  cl_uint num_platforms = 0;
  cl_platform_id platform;
  cl_uint num_devices = 0;
  cl_device_id device;
  cl_command_queue_properties command_queue_properties = 0;

  // Get the number of platforms in the system.
  status = clGetPlatformIDs(0, NULL, &num_platforms);
  if (status != CL_SUCCESS || num_platforms == 0)
    goto fail;

  // Get the platform ID for one platform
  status = clGetPlatformIDs(1, &platform, NULL);
  if (status != CL_SUCCESS)
    goto fail;

  // Get the number of devices available on the platform
  status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &num_devices);
  if (status != CL_SUCCESS || num_devices == 0)
    goto fail;

  // Get the device ID for one device
  status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
  if (status != CL_SUCCESS)
    goto fail;

  // Create OpenCL context for one device
  *context = clCreateContext(NULL, 1, &device, NULL, NULL, &status);
  if (status != CL_SUCCESS || *context == NULL)
    goto fail;

  // Create command queues for the device
  *cmd_queue = clCreateCommandQueue(*context, device, command_queue_properties, &status);
  if (status != CL_SUCCESS || *cmd_queue == NULL)
    goto fail;
  return 0;

fail:
  return 1;
}
#endif

int main(int argc, char **argv) {
  int x, y, z;
  int width = 1280, height = 720;
  unsigned char *buffer[3];
  int use_gpu;
  cl_mem opencl_mem[3];
  cl_context context;
  cl_command_queue cmd_queue;
  cl_int status;

  if (argc != 2)
    return 0;

  use_gpu = atoi(argv[1]);

  if (use_gpu) {
    if (opencl_init(&context, &cmd_queue))
      printf("OpenCL init failure");
  }

  if (use_gpu) {
    for (x = 0; x < 3; x++) {
      opencl_mem[x] = clCreateBuffer(context,
                                     CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR,
                                     width * height * sizeof(*buffer[x]), NULL,
                                     &status);
      if (status != CL_SUCCESS)
        return 0;
      buffer[x] = clEnqueueMapBuffer(cmd_queue, opencl_mem[x], CL_TRUE,
                                     CL_MAP_READ | CL_MAP_WRITE, 0,
                                     width * height * sizeof(*buffer[x]), 0,
                                     NULL, NULL, &status);
      if (status != CL_SUCCESS) {
        clReleaseMemObject(opencl_mem[x]);
        opencl_mem[x] = NULL;
        return 0;
      }
    }
  } else {
    for (x = 0; x < 3; x++) {
      buffer[x] = malloc(width * height * sizeof(*buffer[x]));
      if (buffer[x] == NULL) {
        printf("Unable to alloc memory");
      }
    }
  }

  memset(buffer[0], 1, width * height * sizeof(*buffer[0]));
  memset(buffer[1], 2, width * height * sizeof(*buffer[1]));
  memset(buffer[2], 0, width * height * sizeof(*buffer[2]));

  {
    struct usec_timer emr_timer;
    usec_timer_start(&emr_timer);
    for (z = 0; z < 600; z++) {
      for (y = 0; y < height; y++) {
        for (x = 0; x < width; x++) {
          // don't worry about overflows
          buffer[2][y * width + x] += buffer[0][y * width + x]
                                     + buffer[1][y * width + x];
        }
      }
    }
    usec_timer_mark(&emr_timer);
    printf("Elapsed time %"PRIu64"\n", usec_timer_elapsed(&emr_timer));
  }

  if (use_gpu) {
    for (x = 0; x < 3; x++) {
      if (buffer[x] != NULL) {
        status = clEnqueueUnmapMemObject(cmd_queue, opencl_mem[0], buffer[0], 0,
                                         NULL, NULL);
        status |= clFinish(cmd_queue);
        if (status != CL_SUCCESS)
          return 0;
        buffer[0] = NULL;
      }

      if (opencl_mem[0] != NULL) {
        status = clReleaseMemObject(opencl_mem[0]);
        if (status != CL_SUCCESS)
          return 0;
        opencl_mem[0] = NULL;
      }
    }

    clReleaseCommandQueue(cmd_queue);
    clReleaseContext(context);
  } else {
    for (x = 0; x < 3; x++) {
      free(buffer[x]);
      buffer[x] = NULL;
    }
  }
  return 0;
}

【问题讨论】:

    标签: opencl


    【解决方案1】:

    如果您使用malloc + operation + free,则您仅使用 CPU 资源。

    如果您使用 OpenCL,则您使用的是 CPU + GPU,并且会涉及同步和数据复制惩罚。

    • GPU 中的分配
    • 映射到 CPU 空间(在 CPU 中分配另一个缓冲区)
    • 操作 CPU 缓冲区
    • 取消映射(将副本固定到 GPU 缓冲区 + 释放 CPU 之一)。
    • 销毁 GPU 缓冲区

    是什么让您认为它应该具有相同的速度?当然成本更高,而且永远如此。您正在执行相同的 CPU 操作 + 一些额外的 OpenCL 操作。

    固定内存的传输速度比非固定内存快,但它永远不会比非复制快,因为您根本没有复制任何东西!

    同样对于内存基准,使用 3*1280*720 = 2.6MB 进行操作是完全愚蠢的。在普通系统中只需要几微秒。无论如何,这两种情况的那部分应该是相同的。

    开销将决定您的结果,而不是吞吐量。

    【讨论】:

    • 首先映射到 CPU 空间并不一定会在 CPU 中创建另一个缓冲区。这取决于调用 clEnqueueBuffer() 的参数。如果调用参数是 CL_MEM_ALLOC_HOST_PTR,在 CPU 端我们将只接收一个虚拟内存地址而不是另一个缓冲区。此外,应用程序的分析不是在开始到结束时完成,而是在填充缓冲区 3 时完成。甚至在 memset 中也没有。它只是一个 a[i] = b[i]+c[i] 代码。这就是分析完成的地方,预计这两种情况的时间都相同。
    • 除非您使用的是 OpenCL 2.0 的 SVM,否则地图总是意味着副本。你在中间做了多少操作并不重要,只要它们对两个测试都是通用的。关键是OpenCL的额外开销,对CPU缓冲区的操作应该是完全一样的。
    猜你喜欢
    • 2014-08-01
    • 2015-08-18
    • 2011-08-15
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 2011-04-28
    • 2019-02-21
    • 2015-09-11
    相关资源
    最近更新 更多