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 #ifndef __COMMON_H__
17 #define __COMMON_H__
18
19 #include "harness/compat.h"
20 #include "harness/testHarness.h"
21 #include "harness/errorHelpers.h"
22 #include "harness/kernelHelpers.h"
23 #include "harness/typeWrappers.h"
24 #include <vector>
25 #include <string>
26
27 #if (defined(_WIN32) || defined(_WIN64)) && defined(_MSC_VER)
28 #include <windows.h>
29 #endif
30
31 typedef enum {
32 memory_order_relaxed,
33 memory_order_acquire,
34 memory_order_release,
35 memory_order_acq_rel,
36 memory_order_seq_cst
37 } cl_memory_order;
38
39 cl_int AtomicLoadExplicit(volatile cl_int * pValue, cl_memory_order order);
40 cl_int AtomicFetchAddExplicit(volatile cl_int *object, cl_int operand, cl_memory_order o);
41
42 template <typename T>
AtomicCompareExchangeStrongExplicit(volatile T * a,T * expected,T desired,cl_memory_order order_success,cl_memory_order order_failure)43 bool AtomicCompareExchangeStrongExplicit(volatile T *a, T *expected, T desired,
44 cl_memory_order order_success,
45 cl_memory_order order_failure)
46 {
47 T tmp;
48 #if defined( _MSC_VER ) || (defined( __INTEL_COMPILER ) && defined(WIN32))
49 tmp = (sizeof(void*) == 8) ? (T)InterlockedCompareExchange64((volatile LONG64 *)a, (LONG64)desired, *(LONG64 *)expected) :
50 (T)InterlockedCompareExchange((volatile LONG*)a, (LONG)desired, *(LONG*)expected);
51 #elif defined(__GNUC__)
52 tmp = (T)__sync_val_compare_and_swap((volatile intptr_t*)a, (intptr_t)(*expected), (intptr_t)desired);
53 #else
54 log_info("Host function not implemented: atomic_compare_exchange\n");
55 tmp = 0;
56 #endif
57 if(tmp == *expected)
58 return true;
59 *expected = tmp;
60 return false;
61 }
62
63 // this checks for a NULL ptr and/or an error code
64 #define test_error2(error_code, ptr, msg) { if(error != 0) { test_error(error_code, msg); } else { if(NULL == ptr) {print_null_error(msg); return -1;} } }
65 #define print_null_error(msg) log_error("ERROR: %s! (NULL pointer detected %s:%d)\n", msg, __FILE__, __LINE__ );
66
67 // max possible number of queues needed, 1 for each device in platform.
68 #define MAXQ 32
69
70 typedef struct Node{
71 cl_int global_id;
72 cl_int position_in_list;
73 struct Node* pNext;
74 } Node;
75
76 extern void create_linked_lists(Node* pNodes, size_t num_lists, int list_length);
77 extern cl_int verify_linked_lists(Node* pNodes, size_t num_lists, int list_length);
78
79 extern cl_int create_linked_lists_on_device(int qi, cl_command_queue q, cl_mem allocator, cl_kernel k, size_t numLists );
80 extern cl_int verify_linked_lists_on_device(int qi, cl_command_queue q, cl_mem num_correct, cl_kernel k, cl_int ListLength, size_t numLists );
81 extern cl_int create_linked_lists_on_device_no_map(int qi, cl_command_queue q, size_t *pAllocator, cl_kernel k, size_t numLists );
82 extern cl_int verify_linked_lists_on_device_no_map(int qi, cl_command_queue q, cl_int *pNum_correct, cl_kernel k, cl_int ListLength, size_t numLists );
83
84 extern int test_svm_byte_granularity(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements);
85 extern int test_svm_set_kernel_exec_info_svm_ptrs(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements);
86 extern int test_svm_fine_grain_memory_consistency(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements);
87 extern int test_svm_fine_grain_sync_buffers(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements);
88 extern int test_svm_shared_address_space_coarse_grain_old_api(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements);
89 extern int test_svm_shared_address_space_coarse_grain_new_api(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements);
90 extern int test_svm_shared_address_space_fine_grain_buffers(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements);
91 extern int test_svm_shared_address_space_fine_grain(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements);
92 extern int test_svm_cross_buffer_pointers_coarse_grain(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements);
93 extern int test_svm_pointer_passing(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements);
94 extern int test_svm_allocate_shared_buffer(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements);
95 extern int test_svm_shared_sub_buffers(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements);
96 extern int test_svm_enqueue_api(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements);
97 extern int test_svm_migrate(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements);
98
99 extern cl_int create_cl_objects(cl_device_id device_from_harness, const char** ppCodeString, cl_context* context, cl_program *program, cl_command_queue *queues, cl_uint *num_devices, cl_device_svm_capabilities required_svm_caps, std::vector<std::string> extensions_list = std::vector<std::string>());
100
101 extern const char *linked_list_create_and_verify_kernels[];
102
103 #endif // #ifndef __COMMON_H__
104
105