1 //
2 // Copyright (c) 2017 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #include "common.h"
17
18 const cl_mem_flags flag_set[] = {
19 CL_MEM_READ_WRITE,
20 CL_MEM_WRITE_ONLY,
21 CL_MEM_READ_ONLY,
22 CL_MEM_READ_WRITE | CL_MEM_SVM_FINE_GRAIN_BUFFER,
23 CL_MEM_WRITE_ONLY | CL_MEM_SVM_FINE_GRAIN_BUFFER,
24 CL_MEM_READ_ONLY | CL_MEM_SVM_FINE_GRAIN_BUFFER,
25 CL_MEM_READ_WRITE | CL_MEM_SVM_FINE_GRAIN_BUFFER | CL_MEM_SVM_ATOMICS,
26 CL_MEM_WRITE_ONLY | CL_MEM_SVM_FINE_GRAIN_BUFFER | CL_MEM_SVM_ATOMICS,
27 CL_MEM_READ_ONLY | CL_MEM_SVM_FINE_GRAIN_BUFFER | CL_MEM_SVM_ATOMICS,
28 0
29 };
30 const char* flag_set_names[] = {
31 "CL_MEM_READ_WRITE",
32 "CL_MEM_WRITE_ONLY",
33 "CL_MEM_READ_ONLY",
34 "CL_MEM_READ_WRITE | CL_MEM_SVM_FINE_GRAIN_BUFFER",
35 "CL_MEM_WRITE_ONLY | CL_MEM_SVM_FINE_GRAIN_BUFFER",
36 "CL_MEM_READ_ONLY | CL_MEM_SVM_FINE_GRAIN_BUFFER",
37 "CL_MEM_READ_WRITE | CL_MEM_SVM_FINE_GRAIN_BUFFER | CL_MEM_SVM_ATOMICS",
38 "CL_MEM_WRITE_ONLY | CL_MEM_SVM_FINE_GRAIN_BUFFER | CL_MEM_SVM_ATOMICS",
39 "CL_MEM_READ_ONLY | CL_MEM_SVM_FINE_GRAIN_BUFFER | CL_MEM_SVM_ATOMICS",
40 "0"
41 };
42
43
test_svm_allocate_shared_buffer(cl_device_id deviceID,cl_context context2,cl_command_queue queue,int num_elements)44 int test_svm_allocate_shared_buffer(cl_device_id deviceID, cl_context context2, cl_command_queue queue, int num_elements)
45 {
46 clContextWrapper context = NULL;
47 clProgramWrapper program = NULL;
48 cl_uint num_devices = 0;
49 cl_int err = CL_SUCCESS;
50 clCommandQueueWrapper queues[MAXQ];
51
52 cl_device_svm_capabilities caps;
53 err = clGetDeviceInfo(deviceID, CL_DEVICE_SVM_CAPABILITIES, sizeof(cl_device_svm_capabilities), &caps, NULL);
54 test_error(err,"clGetDeviceInfo failed for CL_DEVICE_SVM_CAPABILITIES");
55
56 // under construction...
57 err = create_cl_objects(deviceID, NULL, &context, &program, &queues[0], &num_devices, CL_DEVICE_SVM_COARSE_GRAIN_BUFFER);
58 if(err) return -1;
59
60 size_t size = 1024;
61
62 // iteration over flag combos
63 int num_flags = sizeof(flag_set)/sizeof(cl_mem_flags);
64 for(int i = 0; i < num_flags; i++)
65 {
66 if (((flag_set[i] & CL_MEM_SVM_FINE_GRAIN_BUFFER) != 0 && (caps & CL_DEVICE_SVM_FINE_GRAIN_BUFFER) == 0)
67 || ((flag_set[i] & CL_MEM_SVM_ATOMICS) != 0 && (caps & CL_DEVICE_SVM_ATOMICS) == 0))
68 {
69 log_info("Skipping clSVMalloc with flags: %s\n", flag_set_names[i]);
70 continue;
71 }
72
73 log_info("Testing clSVMalloc with flags: %s\n", flag_set_names[i]);
74 cl_char *pBufData1 = (cl_char*) clSVMAlloc(context, flag_set[i], size, 0);
75 if(pBufData1 == NULL)
76 {
77 log_error("SVMalloc returned NULL");
78 return -1;
79 }
80
81 {
82 clMemWrapper buf = clCreateBuffer(context, CL_MEM_USE_HOST_PTR, size, pBufData1, &err);
83 test_error(err,"clCreateBuffer failed");
84
85 cl_char *pBufData2 = NULL;
86 cl_uint flags = CL_MAP_READ | CL_MAP_READ;
87 if(flag_set[i] & CL_MEM_HOST_READ_ONLY) flags ^= CL_MAP_WRITE;
88 if(flag_set[i] & CL_MEM_HOST_WRITE_ONLY) flags ^= CL_MAP_READ;
89
90 if(!(flag_set[i] & CL_MEM_HOST_NO_ACCESS))
91 {
92 pBufData2 = (cl_char*) clEnqueueMapBuffer(queues[0], buf, CL_TRUE, flags, 0, size, 0, NULL,NULL, &err);
93 test_error(err, "clEnqueueMapBuffer failed");
94
95 if(pBufData2 != pBufData1 || NULL == pBufData1)
96 {
97 log_error("SVM pointer returned by clEnqueueMapBuffer doesn't match pointer returned by clSVMalloc");
98 return -1;
99 }
100 err = clEnqueueUnmapMemObject(queues[0], buf, pBufData2, 0, NULL, NULL);
101 test_error(err, "clEnqueueUnmapMemObject failed");
102 err = clFinish(queues[0]);
103 test_error(err, "clFinish failed");
104 }
105 }
106
107 clSVMFree(context, pBufData1);
108 }
109
110 return 0;
111 }
112