opencl_test.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. // See boost/compute/detail/diagnostic.hpp
  11. // GCC
  12. #if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
  13. #define BOOST_COMPUTE_GCC_DIAG_STR(s) #s
  14. #define BOOST_COMPUTE_GCC_DIAG_JOINSTR(x,y) BOOST_COMPUTE_GCC_DIAG_STR(x ## y)
  15. # define BOOST_COMPUTE_GCC_DIAG_DO_PRAGMA(x) _Pragma (#x)
  16. # define BOOST_COMPUTE_GCC_DIAG_PRAGMA(x) BOOST_COMPUTE_GCC_DIAG_DO_PRAGMA(GCC diagnostic x)
  17. # if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
  18. # define BOOST_COMPUTE_GCC_DIAG_OFF(x) BOOST_COMPUTE_GCC_DIAG_PRAGMA(push) \
  19. BOOST_COMPUTE_GCC_DIAG_PRAGMA(ignored BOOST_COMPUTE_GCC_DIAG_JOINSTR(-W,x))
  20. # define BOOST_COMPUTE_GCC_DIAG_ON(x) BOOST_COMPUTE_GCC_DIAG_PRAGMA(pop)
  21. # else
  22. # define BOOST_COMPUTE_GCC_DIAG_OFF(x) \
  23. BOOST_COMPUTE_GCC_DIAG_PRAGMA(ignored BOOST_COMPUTE_GCC_DIAG_JOINSTR(-W,x))
  24. # define BOOST_COMPUTE_GCC_DIAG_ON(x) \
  25. BOOST_COMPUTE_GCC_DIAG_PRAGMA(warning BOOST_COMPUTE_GCC_DIAG_JOINSTR(-W,x))
  26. # endif
  27. #else // Ensure these macros do nothing for other compilers.
  28. # define BOOST_COMPUTE_GCC_DIAG_OFF(x)
  29. # define BOOST_COMPUTE_GCC_DIAG_ON(x)
  30. #endif
  31. // Clang
  32. #ifdef __clang__
  33. # define BOOST_COMPUTE_CLANG_DIAG_STR(s) # s
  34. // stringize s to "no-sign-compare"
  35. # define BOOST_COMPUTE_CLANG_DIAG_JOINSTR(x,y) BOOST_COMPUTE_CLANG_DIAG_STR(x ## y)
  36. // join -W with no-unused-variable to "-Wno-sign-compare"
  37. # define BOOST_COMPUTE_CLANG_DIAG_DO_PRAGMA(x) _Pragma (#x)
  38. // _Pragma is unary operator #pragma ("")
  39. # define BOOST_COMPUTE_CLANG_DIAG_PRAGMA(x) \
  40. BOOST_COMPUTE_CLANG_DIAG_DO_PRAGMA(clang diagnostic x)
  41. # define BOOST_COMPUTE_CLANG_DIAG_OFF(x) BOOST_COMPUTE_CLANG_DIAG_PRAGMA(push) \
  42. BOOST_COMPUTE_CLANG_DIAG_PRAGMA(ignored BOOST_COMPUTE_CLANG_DIAG_JOINSTR(-W,x))
  43. // For example: #pragma clang diagnostic ignored "-Wno-sign-compare"
  44. # define BOOST_COMPUTE_CLANG_DIAG_ON(x) BOOST_COMPUTE_CLANG_DIAG_PRAGMA(pop)
  45. // For example: #pragma clang diagnostic warning "-Wno-sign-compare"
  46. #else // Ensure these macros do nothing for other compilers.
  47. # define BOOST_COMPUTE_CLANG_DIAG_OFF(x)
  48. # define BOOST_COMPUTE_CLANG_DIAG_ON(x)
  49. # define BOOST_COMPUTE_CLANG_DIAG_PRAGMA(x)
  50. #endif
  51. // MSVC
  52. #if defined(_MSC_VER)
  53. # define BOOST_COMPUTE_MSVC_DIAG_DO_PRAGMA(x) __pragma(x)
  54. # define BOOST_COMPUTE_MSVC_DIAG_PRAGMA(x) \
  55. BOOST_COMPUTE_MSVC_DIAG_DO_PRAGMA(warning(x))
  56. # define BOOST_COMPUTE_MSVC_DIAG_OFF(x) BOOST_COMPUTE_MSVC_DIAG_PRAGMA(push) \
  57. BOOST_COMPUTE_MSVC_DIAG_PRAGMA(disable: x)
  58. # define BOOST_COMPUTE_MSVC_DIAG_ON(x) BOOST_COMPUTE_MSVC_DIAG_PRAGMA(pop)
  59. #else // Ensure these macros do nothing for other compilers.
  60. # define BOOST_COMPUTE_MSVC_DIAG_OFF(x)
  61. # define BOOST_COMPUTE_MSVC_DIAG_ON(x)
  62. #endif
  63. #include <iostream>
  64. // include the proper opencl header for the system
  65. #if defined(__APPLE__)
  66. #include <OpenCL/cl.h>
  67. #else
  68. #include <CL/cl.h>
  69. #endif
  70. // the opencl_test example displays the opencl platforms and devices found
  71. // on the system using the opencl api directly. if this test fails to compile
  72. // and/or run, there is a problem with the opencl implementation found on the
  73. // system. users should ensure this test runs successfuly before using any of
  74. // the boost.compute apis (which depend on a working opencl implementation).
  75. int main()
  76. {
  77. // Suppress deprecated declarations warning
  78. BOOST_COMPUTE_MSVC_DIAG_OFF(4996); // MSVC
  79. BOOST_COMPUTE_GCC_DIAG_OFF(deprecated-declarations); // GCC
  80. BOOST_COMPUTE_CLANG_DIAG_OFF(deprecated-declarations); // Clang
  81. // query number of opencl platforms
  82. cl_uint num_platforms = 0;
  83. cl_int ret = clGetPlatformIDs(0, NULL, &num_platforms);
  84. if(ret != CL_SUCCESS){
  85. std::cerr << "failed to query platforms: " << ret << std::endl;
  86. return -1;
  87. }
  88. // check that at least one platform was found
  89. if(num_platforms == 0){
  90. std::cerr << "found 0 platforms" << std::endl;
  91. return 0;
  92. }
  93. // get platform ids
  94. cl_platform_id *platforms = new cl_platform_id[num_platforms];
  95. clGetPlatformIDs(num_platforms, platforms, NULL);
  96. // iterate through each platform and query its devices
  97. for(cl_uint i = 0; i < num_platforms; i++){
  98. cl_platform_id platform = platforms[i];
  99. // query number of opencl devices
  100. cl_uint num_devices = 0;
  101. ret = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices);
  102. if(ret != CL_SUCCESS){
  103. std::cerr << "failed to lookup devices for platform " << i << std::endl;
  104. continue;
  105. }
  106. // print number of devices found
  107. std::cout << "platform " << i << " has " << num_devices << " devices:" << std::endl;
  108. // get device ids for the platform
  109. cl_device_id *devices = new cl_device_id[num_devices];
  110. ret = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, num_devices, devices, NULL);
  111. if(ret != CL_SUCCESS){
  112. std::cerr << "failed to query platform devices" << std::endl;
  113. delete[] devices;
  114. continue;
  115. }
  116. // iterate through each device on the platform and print its name
  117. for(cl_uint j = 0; j < num_devices; j++){
  118. cl_device_id device = devices[j];
  119. // get length of the device name string
  120. size_t name_length = 0;
  121. ret = clGetDeviceInfo(device, CL_DEVICE_NAME, 0, NULL, &name_length);
  122. if(ret != CL_SUCCESS){
  123. std::cerr << "failed to query device name length for device " << j << std::endl;
  124. continue;
  125. }
  126. // get the device name string
  127. char *name = new char[name_length];
  128. ret = clGetDeviceInfo(device, CL_DEVICE_NAME, name_length, name, NULL);
  129. if(ret != CL_SUCCESS){
  130. std::cerr << "failed to query device name string for device " << j << std::endl;
  131. delete[] name;
  132. continue;
  133. }
  134. // print out the device name
  135. std::cout << " device: " << name << std::endl;
  136. delete[] name;
  137. }
  138. delete[] devices;
  139. }
  140. delete[] platforms;
  141. BOOST_COMPUTE_CLANG_DIAG_ON(deprecated-declarations); // Clang
  142. BOOST_COMPUTE_GCC_DIAG_ON(deprecated-declarations); // GCC
  143. BOOST_COMPUTE_MSVC_DIAG_OFF(4996); // MSVC
  144. return 0;
  145. }