cl_getdeviceids.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012, 2016 University of Bordeaux
  4. * Copyright (C) 2012 CNRS
  5. * Copyright (C) 2012 Vincent Danjean <Vincent.Danjean@ens-lyon.org>
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include "socl.h"
  19. #include "init.h"
  20. /**
  21. * \brief Return one device of each kind
  22. *
  23. * \param[in] platform Must be StarPU platform ID or NULL
  24. */
  25. CL_API_ENTRY cl_int CL_API_CALL
  26. soclGetDeviceIDs(cl_platform_id platform,
  27. cl_device_type device_type,
  28. cl_uint num_entries,
  29. cl_device_id * devices,
  30. cl_uint * num_devices) CL_API_SUFFIX__VERSION_1_0
  31. {
  32. if( ! _starpu_init )
  33. if (socl_init_starpu() < 0)
  34. {
  35. *num_devices = 0;
  36. return CL_SUCCESS;
  37. }
  38. if (_starpu_init_failed) {
  39. *num_devices = 0;
  40. return CL_SUCCESS;
  41. }
  42. if (platform != NULL && platform != &socl_platform)
  43. return CL_INVALID_PLATFORM;
  44. if ((devices != NULL && num_entries == 0)
  45. || (devices == NULL && num_devices == NULL))
  46. return CL_INVALID_VALUE;
  47. if (!(device_type & (CL_DEVICE_TYPE_CPU | CL_DEVICE_TYPE_GPU | CL_DEVICE_TYPE_ACCELERATOR | CL_DEVICE_TYPE_DEFAULT))
  48. && (device_type != CL_DEVICE_TYPE_ALL))
  49. return CL_INVALID_DEVICE_TYPE;
  50. int ndevs = starpu_worker_get_count_by_type(STARPU_OPENCL_WORKER);
  51. int workers[ndevs];
  52. starpu_worker_get_ids_by_type(STARPU_OPENCL_WORKER, workers, ndevs);
  53. if (socl_devices == NULL) {
  54. socl_device_count = ndevs;
  55. socl_devices = malloc(sizeof(struct _cl_device_id) * ndevs);
  56. int i;
  57. for (i=0; i < ndevs; i++) {
  58. int devid = starpu_worker_get_devid(workers[i]);
  59. socl_devices[i].dispatch = &socl_master_dispatch;
  60. socl_devices[i].worker_id = workers[i];
  61. socl_devices[i].device_id = devid;
  62. }
  63. }
  64. int i;
  65. unsigned int num = 0;
  66. for (i=0; i < ndevs; i++) {
  67. int devid = socl_devices[i].device_id;
  68. cl_device_id dev;
  69. starpu_opencl_get_device(devid, &dev);
  70. cl_device_type typ;
  71. clGetDeviceInfo(dev, CL_DEVICE_TYPE, sizeof(typ), &typ, NULL);
  72. if (typ & device_type) {
  73. if (devices != NULL && num < num_entries) devices[num] = &socl_devices[i];
  74. num++;
  75. }
  76. }
  77. if (num_devices != NULL)
  78. *num_devices = num;
  79. return CL_SUCCESS;
  80. }