cl_getdeviceids.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2012 Vincent Danjean
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include "socl.h"
  18. #include "init.h"
  19. /**
  20. * \brief Return one device of each kind
  21. *
  22. * \param[in] platform Must be StarPU platform ID or NULL
  23. */
  24. CL_API_SUFFIX__VERSION_1_0
  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)
  31. {
  32. if (socl_init_starpu() < 0)
  33. {
  34. *num_devices = 0;
  35. return CL_SUCCESS;
  36. }
  37. if (_starpu_init_failed)
  38. {
  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. {
  55. socl_device_count = ndevs;
  56. socl_devices = malloc(sizeof(struct _cl_device_id) * ndevs);
  57. int i;
  58. for (i=0; i < ndevs; i++)
  59. {
  60. int devid = starpu_worker_get_devid(workers[i]);
  61. socl_devices[i].dispatch = &socl_master_dispatch;
  62. socl_devices[i].worker_id = workers[i];
  63. socl_devices[i].device_id = devid;
  64. }
  65. }
  66. int i;
  67. unsigned int num = 0;
  68. for (i=0; i < ndevs; i++)
  69. {
  70. int devid = socl_devices[i].device_id;
  71. cl_device_id dev;
  72. starpu_opencl_get_device(devid, &dev);
  73. cl_device_type typ;
  74. clGetDeviceInfo(dev, CL_DEVICE_TYPE, sizeof(typ), &typ, NULL);
  75. if (typ & device_type)
  76. {
  77. if (devices != NULL && num < num_entries) devices[num] = &socl_devices[i];
  78. num++;
  79. }
  80. }
  81. if (num_devices != NULL)
  82. *num_devices = num;
  83. return CL_SUCCESS;
  84. }