본문 바로가기
멀티코어 프로그래밍/OpenCL

OpenCL 플랫폼, 디바이스, 컨텍스트

by 기리의 개발로그 2015. 7. 25.

openCL 플랫폼, 디바이스, 컨텍스트



플랫폼 모델



OpenCL 플랫폼


- 플랫폼

- openCL 구현

- 시스템에 여러 벤더의 opencl 구현이 설치되어 있다면

- 플랫폼이 여러 개가 존재한다는 의미

- ex) AMD opencl, intel opencl



플랫폼(Platform)


cl_int clGetPlatformIDs(cl_uint num_entries, cl_platform_id *platforms, cl_uint *num_platforms) 


- OpenCL 플랫폼의 ID를 가져옴


- num_entires

- 요청하고자 하는 플랫폼의 수


- platforms

- 존재하는 플랫폼들에 대한 포인터


- num_platforms

- 존재하는 플랫폼의 수


- 리턴 값

- cl_int 에러 정보(음수 값)

- CL_SUCCESS(0)


- 사용 예

- err = clGetPlatformIDs(0, NULL, &num_platforms)         // 실제 존재하는 platform 수를 얻음

- err = clGetPlatformIDs(num_platforms, &platform, NULL)


- Function-Allocation-Function

- 일반적으로는 플랫폼의 개수를 모름

- 다음 3단계 절차를 이용

- 플랫폼 개수 알아오기

- 메모리 할당

- 플랫폼 얻어오기

- 코드 패턴

1. cl_platform_id *platforms;

2. cl_uint num_platforms;

3. cl_GetPlatformIDs(1, NULL, &num_platforms);

4. platforms = (cl_platform_id *)malloc(sizeof(cl_platform_id) * num_platforms);

5. cl_GetPlatformIDs(num_platforms, platforms, NULL);



cl_int clGetPlatformInfo(cl_platform_id platform, cl_platform_info param_name, size_t param_value_size, void                                      *param_value, size_t *param_value_size_ret) 


- OpenCL 플랫폼의 정보를 얻어옴


- patform

- clGetPlatformIDs()로 얻어온 플랫폼 ID


- param_name

- 얻고자 하는 정보 이름


- param_value_size

- param_value가 가리키는 메모리의 크기, param_value 가 NULL이면 무시됨


- param_value

- 플랫폼 정보가 저장될 메모리의 포인터


- param_value_size_ret

- param_value의 실제 크기


- 리턴 값

- cl_int 에러 정보(음수 값)

- CL_SUCCESS(0) 


- 사용 예

- char pform_vendor[40];

- cl_GetPlatformInfo(platforms[0], CL_PLATFORM_VENDOR, sizeof(pform_vendor), pform_vendor, NULL);



디바이스(Device)


cl_int clGetDeviceIDs(cl_platform_id platform, cl_device_type_type device_type, cl_uint num_entries, cl_device_id                                    *devices, cl_uint *num_devices) 


- OpenCL 계산 디바이스의 ID를 가져옴


- platform

- clGetPlatformIDs()를 이용해 가져온 플랫폼


- device_type

- 사용하고자 하는 OpenCL 계산 디바이스의 종류

- ex) CL_DEVICE_TYPE_CPU, CL_DEVICE_TYPE_GPU, CL_DEVICE_TYPE_DEFAULT 등


- num_entries


- devices

-


- num_devices

-


- 사용 예

- cl_device_id devs[3];

- cl_GetDeviceIDs(plat, CL_DEVICE_TYPE_CPU, 3, devs, NULL);


- cl_uint  num_devices;

- cl_GetDeviceIDs(plat, CL_DEVICE_TYPE_GPU, 1, NULL, &num_devices);



cl_int clGetDeviceInfo(cl_device_id device, cl_device_info param_name, size_t param_value_size, void                                   *param_value, size_t *param_value_size_ret) 


- param_name에 해당하는 디바이스의 정보를 얻어 옴


- device

- 디바이스 ID


- param_name

- 얻어오고자 하는 정보 이름


- param_value_size

- param_value가 가리키는 메모리의 크기


- param_value

- 디바이스 정보가 저장될 메모리의 포인터


- param_value_size_ret

- 디바이스 정보의 실제 크기



컨텍스트(Context)


- OpenCL 컨텍스트

- 커널이 실행되는 황경

- 동기화와 메모리 관리가 정의되는 도메인

- 호스트 프로그램이 API clCreateContext()를 이용하여 생성


- 포함하는 리소스

- 디바이스

- 호스트가 사용할 OpenCL 디바이스 목록

- 커널

- OpenCL 디바이스에서 실행할 OpenCL 함수들

- 프로그램 오브젝트

- 프로그램 소스 및 실행 오브젝트

- 메모리 오브젝트

- 호스트와 OpenCL 디바이스에서 접근 가능한 메모리 오브젝트 집합

- 커널은 계산 과정에서 메모리 오브젝트 사용



 cl_context clCreateContext(const cl_context_properties *properties, cl_uint num_devices, const cl_device_id *device,                                          void (CL_CALLBACK *pfn_notify) (const char *errinfo, const void *private_info, size_t cb,                                            void *user_data), void *user_data, cl_int *errcode_ret)


- properties

- 사용하고자 하는 컨텍스트의 속성 결정

- CL_CONTEXT_PLATFORM

- cl_platform_id 값을 통해 사용하고자 하는 플랫폼 정의

- CL_CONTEXT_INTEROP_USER_SYNC

- boolean 값으로 OpenCL extension 의 다른  API들과의 동기화 관련 설정

- 마지막 값은 항상 0

- 사용 예

- properties[0] = CL_CONTEXT_PLATFORM;

- properties[1] = platform;

- properties[2] = 0;


- num_devices


- devices

-


- pfn_notify

- 컨텍스트를 만드는 과정에서 에러가 발생하는 경우 사용자에게 알리기 위해 사용

- errinfo

- 기본적인 error 정보

- private_info

- 추가 정보

- cb

- private_info 의 크기

- user_data

- pfn_notify 에 사용자가 전하고자 하는 파라미터


- errcode_ret

- 에러 정보



 cl_context clCreateContextFromType(const cl_context_properties *properties, cl_device_type device_type, void                                                                (CL_CALLBACK *pfn_notify) (const char *errinfo, const void *private_info,

                                                     size_t cb, void *user_data), void *user_data, cl_int *errcode_ret)


- 컨텍스트를 만드는 다른 함수


- 디바이스 타입만을 지정하여 컨텍스트를 생성


- clGetDeviceIDs()로 얻을 수 있는 디바이스만을 이용

- 서브 디바이스는 사용하지 않음


- 사용 예

- cl_context context = clCreateContextFromType(context_props, CL_DEVICE_TYPE_GPU, NULL, NULL, &err);



 cl_int clGetContextInfo(cl_context context, cl_context_info param_name, size_t param_value_size, void *param_value,

                                 size_t *param_value_size_ret)



컨텍스트와 참조 개수(Reference Count)


- Reference Count

- 컨텍스트가 몇 번 참조되었는지 나타내 줌

- 0이 되면 메모리가 해제됨

- 컨텍스트를 라이브러리나 다른 코드에서 사용하고 싶은 때, reference count 를 0보다 크게 유지해야 함


- 변화 방법

- cl_int clRetainContext(cl_context, context)

- context의 reference count를 1 증가 시킴

- clCreateContext()와 clCreateContextFromType()은 내부적으로 retain을 수행

- cl_int clReleaseContext(cl_context context)

- context의 reference count를 1 감소 시킴

- context의 reference count가 0이고 context에 연관된 오브젝트(ex : 메모리 오브젝트, 커맨드 큐)가 해제되면 context도 해제됨


반응형

'멀티코어 프로그래밍 > OpenCL' 카테고리의 다른 글

OpenCL 프로그래밍(2)  (0) 2015.07.25
OpenCL 프로그램, 커널, 커맨드 큐  (0) 2015.07.25
OpenCL 프로그래밍(1)  (0) 2015.07.25
OpenCL 호스트 프로그램  (0) 2015.07.25
OpenCL Architecture  (0) 2015.07.23

댓글