cl_getdeviceids.c 2.7 KB

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