opencl 相关问题

OpenCL(开放计算语言)是一个用于编写程序的框架,这些程序跨CPU,GPU和其他处理器组成的异构平台执行。

如何使用 OpenCL C++ API 获取设备 cl_device_id

我开始使用 OpenCL C++ API。我已经使用 C API 很长时间了。 C++ API 更加优雅、简单,代码更少且不易出错,但我需要设备 ID。我...

回答 1 投票 0

在 OpenCL 内核中使用模板化结构作为参数

我遇到了一个问题,如果您能帮助我解决问题,我将非常感激。 我尝试使用 clang-17 编译下面看到的代码片段,但出现错误:

回答 1 投票 0

OpenCL 大数取模

我正在尝试在 OpenCL 中计算 mod b,其中 a 是 ulong 元素的数组,并且是 b 长度的两倍。 __kernel void mod(__global ulong *a, __global ulong *b, __global ulong 长度) { ...

回答 2 投票 0

完整的 .NET OpenCL 实现 [已关闭]

我一直在寻找,但运气不佳。 OpenCL 是否有任何有据可查的 .NET 绑定实现? (如果有必要的话,我会为 CUDA 采取一些措施)。 我遇到过各种各样的情况

回答 7 投票 0

有没有办法在OpenCl中加载大小等于GPU全局内存大小的向量?

我的 GPU 有 12 GB 全局内存 (CL_DEVICE_GLOBAL_MEM_SIZE),但只能分配 3 GB 内存 (CL_DEVICE_MAX_MEM_ALLOC_SIZE)。当我尝试加载大小超过 3 GB 的向量时,pro...

回答 1 投票 0

AMD RDNA3指令名称解读

我正在尝试分析为 RDNA3 AMD GPU 编译的 OpenCL 内核。 为此,我使用 Radeon GPU 分析器。 当我在分析器中加载 OpenCL 内核时,它会显示汇编指令...

回答 2 投票 0

安装 vortex 时出现 libhwloc.so.5 错误

安装所有依赖项后在 vortex 中尝试“make”时,出现以下错误: /usr/bin/ld:警告:/opt/pocl/runtime/lib/libOpenCL.so 需要的 libhwloc.so.5,未找到(尝试

回答 1 投票 0

OpenCL 的函数 clGetPlatformInfo 运行时问题。 Win10、VS2012

我的硬件是 Intel Core I7 6700、32 GB RAM、Nvidia GT1030。我的软件是Windows 10 64位,Visual Studio 2012 professional。我买了《OpenCL并行编程开发手册》这本书。

回答 1 投票 0

使用 Imagination IMG GPU 清除 OpenCL 缓冲区

我可以在 Intel、NVIDIA 和 AMD GPU 上使用 clEnqueueFillBuffer() 成功清除 OpenCL 缓冲区对象。 当我尝试在 Imagination IMG GPU 上执行相同操作时,我的程序出现段错误。 它可能...

回答 1 投票 0

链接器找不到 clGetPlatformIDs()?

我正在尝试使用 AMD OCL SDK 编译一个简单的 OpenCL 测试程序: #包括 #包括 int main() { cl_platform_id 平台; cl_device_id 设备; c...

回答 1 投票 0

在opencl中如何在if/while块中同步线程

OpenCL 标准表示在 if/while 块内使用屏障会导致未定义的行为。但我不明白为什么在 if/while 块中同步线程是不可能的;分歧的三...

回答 2 投票 0

在哪里可以找到 rocprofiler 派生的计数器描述和示例

我的 opencl 内核花了太长时间才能完成,rocprof 给了我以下计数器: |索引 |内核名称 | GPU id |队列 ID |队列索引 | PID | tid | GRD |工作组 | LDS | SCR |拱_vgpr |累积...

回答 1 投票 0

C++ OpenCL 只找到 iGPU 但找不到 CPU

正如标题所示,OpenCL API 只检测我的 Intel iGPU,而不检测 CPU 本身。为什么有什么想法吗?我已经通过包管理器安装了 Intel-opencl-icd,但它似乎不是 e...

回答 1 投票 0

Intel GPU 上是否有等效的“扭曲尺寸”或“波前宽度”

在 Nvidia 上,将 CL_DEVICE_WARP_SIZE_NV 查询放入 clGetDeviceInfo 中,通常返回 32。 在 AMD 上,CL_DEVICE_WAVEFRONT_WIDTH_AMD 过去是 64,现在在更新的 AMD GP 上...

回答 1 投票 0

2D 前向 FFT 使用 clFFT 库不会产生结果

我想使用 clFFT 库执行 2D 实数到复数 FFT。输出数组只为实部和虚部保留零,这是不正确的(我有一个使用...实现的工作版本

回答 1 投票 0

为什么这个简单的 openCl 内核的矢量化会使其变慢?

我目前正在使用 OpenCl 在 GPU 上实现一个简单的并行索引总和。这是需要对大型数组进行连续流压缩的项目的一部分,所以我认为这将是一个很好的

回答 1 投票 0

使用 cl::NDRange 具有不同的维度来对内核进行排队是否有效?

例如,如果全局尺寸为 cl::NDRange{1024x1024},则局部尺寸为 cl::NDRange{32} 是否有效? 如果提供了尺寸,程序肯定会运行,但是……

回答 1 投票 0

在 GPU 上运行密集数学计算的最佳方式

我正在做一些学术/教育研究(主题与布尔逻辑及其扩展有关),并且有一个数学算法,我想在一些大的数字集上验证......

回答 1 投票 0

如何打印 OpenCL 警告

我用 opencl c++ 绑定制作了一个 C++ 程序。它在我的 nvidia gpu 上运行良好,但是当使用 oclgrind 进行调试时,我收到生成的消息 6 警告。我尝试使用program.getBuild...

回答 1 投票 0

使用 C 的 OpenCL,run.sh 权限被拒绝

#define _GNU_SOURCE #include“matmul.h” #include“util.h” #define CL_TARGET_OPENCL_VERSION 120 #define CL_USE_DEPRECATED_OPENCL_2_2_APIS #包括 #包括 #define _GNU_SOURCE #include "matmul.h" #include "util.h" #define CL_TARGET_OPENCL_VERSION 120 #define CL_USE_DEPRECATED_OPENCL_2_2_APIS #include <CL/cl.h> #include <stdio.h> #include <stdlib.h> #define CHECK_ERROR(err) \ if (err != CL_SUCCESS) { \ printf("[%s:%d] OpenCL error %d\n", __FILE__, __LINE__, err); \ exit(EXIT_FAILURE); \ } static cl_int err; static cl_platform_id platform; static cl_device_id device; static cl_context context; static cl_command_queue queue; static cl_program program; static cl_kernel kernel; static cl_mem a_d, b_d, c_d; void matmul(const float *A, const float *B, float *C, int M, int N, int K) { for (int m=0; m<M; m++) { for (int n=0; n<N; n++) { float acc = 0.0f; for (int k=0; k<K; k++) { acc += A[k*M + m] * B[n*K + k]; } C[n*M + m] = acc; } } } static void print_platform_info(cl_platform_id platform) { size_t sz; char *buf; CHECK_ERROR(clGetPlatformInfo(platform, CL_PLATFORM_NAME, 0, NULL, &sz)); buf = (char *)malloc(sz); CHECK_ERROR(clGetPlatformInfo(platform, CL_PLATFORM_NAME, sz, buf, NULL)); printf("Detected OpenCL platform: %s\n", buf); free(buf); } static void print_device_info(cl_device_id device) { size_t sz; char *buf; CHECK_ERROR(clGetDeviceInfo(device, CL_DEVICE_NAME, 0, NULL, &sz)); buf = (char *)malloc(sz); CHECK_ERROR(clGetDeviceInfo(device, CL_DEVICE_NAME, sz, buf, NULL)); printf("Detected OpenCL device: %s\n", buf); free(buf); } static cl_program create_and_build_program_with_source(cl_context context, cl_device_id device, const char *file_name) { FILE *file = fopen(file_name, "rb"); if (file == NULL) { printf("Failed to open %s\n", file_name); exit(EXIT_FAILURE); } fseek(file, 0, SEEK_END); size_t source_size = ftell(file); rewind(file); char *source_code = (char *)malloc(source_size + 1); size_t ntotal = 0; while (ntotal < source_size) { int nread = fread(source_code, sizeof(char), source_size, file); ntotal += nread; } source_code[source_size] = '\0'; fclose(file); cl_program program = clCreateProgramWithSource( context, 1, (const char **)&source_code, &source_size, &err); CHECK_ERROR(err); free(source_code); err = clBuildProgram(program, 1, &device, "", NULL, NULL); if (err == CL_BUILD_PROGRAM_FAILURE) { size_t log_size; CHECK_ERROR(clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size)); char *log = (char *)malloc(log_size + 1); CHECK_ERROR(clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, log_size, log, NULL)); log[log_size] = 0; printf("Compile error:\n%s\n", log); free(log); } CHECK_ERROR(err); return program; } void matmul_initialize(int M, int N, int K) { // Get OpenCL platform err = clGetPlatformIDs(1, &platform, NULL); CHECK_ERROR(err); print_platform_info(platform); // Get OpenCL device (only 1) err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL); CHECK_ERROR(err); print_device_info(device); // Create OpenCL context context = clCreateContext(NULL, 1, &device, NULL, NULL, &err); CHECK_ERROR(err); // Create OpenCL command queue queue = clCreateCommandQueue(context, device, 0, &err); CHECK_ERROR(err); // Compile program from "kernel.cl" program = create_and_build_program_with_source(context, device, "kernel.cl"); // Extract kernel from compiled program kernel = clCreateKernel(program, "sgemm", &err); CHECK_ERROR(err); // Create GPU buffers a_d = clCreateBuffer(context, CL_MEM_READ_WRITE, M * K * sizeof(float), NULL, &err); CHECK_ERROR(err); b_d = clCreateBuffer(context, CL_MEM_READ_WRITE, K * N * sizeof(float), NULL, &err); CHECK_ERROR(err); c_d = clCreateBuffer(context, CL_MEM_READ_WRITE, M * N * sizeof(float), NULL, &err); CHECK_ERROR(err); } void matmul_finalize() { clReleaseMemObject(a_d); clReleaseMemObject(b_d); clReleaseMemObject(c_d); clReleaseCommandQueue(queue); clReleaseContext(context); clReleaseProgram(program); clReleaseKernel(kernel); } 这个是matmul.c, #include "util.h" #include <math.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> static double start_time[8]; void timer_init() { srand(time(NULL)); } static double get_time() { struct timespec tv; clock_gettime(CLOCK_MONOTONIC, &tv); return tv.tv_sec + tv.tv_nsec * 1e-9; } void timer_start(int i) { start_time[i] = get_time(); } double timer_stop(int i) { return get_time() - start_time[i]; } void alloc_mat(float **m, int R, int S) { *m = (float *)aligned_alloc(32, sizeof(float) * R * S); if (*m == NULL) { printf("Failed to allocate memory for mat.\n"); exit(0); } } void rand_mat(float *m, int R, int S) { int N = R * S; for (int j = 0; j < N; j++) { m[j] = (float)rand() / RAND_MAX - 0.5; } } void zero_mat(float *m, int R, int S) { int N = R * S; memset(m, 0, sizeof(float) * N); } void print_mat(float *m, int M, int N) { for (int i = 0; i < M; ++i) { for (int j = 0; j < N; ++j) { printf("%+.3f ", m[i * N + j]); } printf("\n"); } } void check_mat_mul(float *A, float *B, float *C, int M, int N, int K) { printf("Validating...\n"); float *C_ans; alloc_mat(&C_ans, M, N); zero_mat(C_ans, M, N); #pragma omp parallel for for (int i = 0; i < M; ++i) { for (int k = 0; k < K; ++k) { for (int j = 0; j < N; ++j) { C_ans[i * N + j] += A[i * K + k] * B[k * N + j]; } } } bool is_valid = true; int cnt = 0, thr = 10; float eps = 1e-3; for (int i = 0; i < M; ++i) { for (int j = 0; j < N; ++j) { float c = C[i * N + j]; float c_ans = C_ans[i * N + j]; if (fabsf(c - c_ans) > eps && (c_ans == 0 || fabsf((c - c_ans) / c_ans) > eps)) { ++cnt; if (cnt <= thr) printf("C[%d][%d] : correct_value = %f, your_value = %f\n", i, j, c_ans, c); if (cnt == thr + 1) printf("Too many error, only first %d values are printed.\n", thr); is_valid = false; } } } if (is_valid) { printf("Result: VALID\n"); } else { printf("Result: INVALID\n"); } } 这个是utils.c, #include <getopt.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "matmul.h" #include "util.h" static void print_help(const char *prog_name) { printf("Usage: %s [-pvh] [-n num_iterations] M N K\n", prog_name); printf("Options:\n"); printf(" -p : print matrix. (default: off)\n"); printf(" -v : validate matrix multiplication. (default: off)\n"); printf(" -h : print this page.\n"); printf(" -n : number of iterations (default: 1)\n"); printf(" M : number of rows of matrix A and C. (default: 8)\n"); printf(" N : number of columns of matrix B and C. (default: 8)\n"); printf( " K : number of columns of matrix A and rows of B. (default: 8)\n"); } static bool print_data = false; static bool validation = false; static int M = 8; static int N = 8; static int K = 8; static int num_iterations = 1; static void parse_opt(int argc, char **argv) { int c; while ((c = getopt(argc, argv, "pvht:n:m:")) != -1) { switch (c) { case 'p': print_data = true; break; case 'v': validation = true; break; case 'n': num_iterations = atoi(optarg); break; case 'h': default: print_help(argv[0]); exit(0); } } for (int i = optind, j = 0; i < argc; ++i, ++j) { switch (j) { case 0: M = atoi(argv[i]); break; case 1: N = atoi(argv[i]); break; case 2: K = atoi(argv[i]); break; default: break; } } printf("Options:\n"); printf(" Problem size: M = %d, N = %d, K = %d\n", M, N, K); printf(" Number of iterations: %d\n", num_iterations); printf(" Print matrix: %s\n", print_data ? "on" : "off"); printf(" Validation: %s\n", validation ? "on" : "off"); printf("\n"); } int main(int argc, char **argv) { parse_opt(argc, argv); printf("Initializing... "); fflush(stdout); float *A, *B, *C; // Initialize random seed timer_init(); // Allocate matrices alloc_mat(&A, M, K); alloc_mat(&B, K, N); alloc_mat(&C, M, N); // Set each element to a random value rand_mat(A, M, K); rand_mat(B, K, N); printf("done!\n"); // Initialize OpenCL printf("Initializing OpenCL...\n"); fflush(stdout); matmul_initialize(M, N, K); // Few warmup iterations zero_mat(C, M, N); for (int i = 0; i < 3; i++) { matmul(A, B, C, M, N, K); } double elapsed_time_sum = 0; for (int i = 0; i < num_iterations; ++i) { printf("Calculating...(iter=%d) ", i); fflush(stdout); zero_mat(C, M, N); timer_start(0); matmul(A, B, C, M, N, K); double elapsed_time = timer_stop(0); printf("%f sec\n", elapsed_time); elapsed_time_sum += elapsed_time; } if (print_data) { printf("MATRIX A:\n"); print_mat(A, M, K); printf("MATRIX B:\n"); print_mat(B, K, N); printf("MATRIX C:\n"); print_mat(C, M, N); } // Finalize OpenCL matmul_finalize(); if (validation) { check_mat_mul(A, B, C, M, N, K); } double elapsed_time_avg = elapsed_time_sum / num_iterations; printf("Avg. time: %f sec\n", elapsed_time_avg); printf("Avg. throughput: %f GFLOPS\n", 2.0 * M * N * K / elapsed_time_avg / 1e9); return 0; } 这是主要的。c 我通过运行这段代码 #!/bin/bash srun --nodes=1 --exclusive --partition=class1 --gres=gpu:4 ./main $@ 在 GPU 集群服务器上,但它给了我 bash:./run_performance.sh:权限被拒绝 这个错误。 我认为GPU的访问权限没有问题。 我可以解决这个问题吗? 我不确定但是 #define CL_TARGET_OPENCL_VERSION 120 #define CL_USE_DEPRECATED_OPENCL_2_2_APIS 这个版本的问题似乎是问题。 这是服务器的GPU信息 Number of platforms 1 Platform Name NVIDIA CUDA Platform Vendor NVIDIA Corporation Platform Version OpenCL 3.0 CUDA 11.8.88 Platform Profile FULL_PROFILE Platform Extensions cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_fp64 cl_khr_3d_image_writes cl_khr_byte_addressable_store cl_khr_icd cl_khr_gl_sharing cl_nv_compiler_options cl_nv_device_attribute_query cl_nv_pragma_unroll cl_nv_copy_opts cl_nv_create_buffer cl_khr_int64_base_atomics cl_khr_int64_extended_atomics cl_khr_device_uuid cl_khr_pci_bus_info cl_khr_external_semaphore cl_khr_external_memory cl_khr_external_semaphore_opaque_fd cl_khr_external_memory_opaque_fd Platform Host timer resolution 0ns Platform Extensions function suffix NV Platform Name NVIDIA CUDA Number of devices 1 Device Name NVIDIA TITAN RTX Device Vendor NVIDIA Corporation Device Vendor ID 0x10de Device Version OpenCL 3.0 CUDA Driver Version 520.61.05 Device OpenCL C Version OpenCL C 1.2 Device Type GPU 更改 matmul.c 的 #define 部分的版本和 API 版本 看起来run_performance.sh没有执行文件权限。在Linux中,每个文件都有单独的读/写/执行权限,并且默认情况下执行权限是关闭的,以避免文件的意外执行和对系统的潜在危害。 就您而言,您希望该文件可执行。要解决这个问题,请运行: chmod +x run_performance.sh 之后你可以执行它: ./run_performance.sh

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.