cl_getdeviceids.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 (socl_init_starpu() < 0)
  33. {
  34. *num_devices = 0;
  35. return CL_SUCCESS;
  36. }
  37. if (_starpu_init_failed) {
  38. *num_devices = 0;
  39. return CL_SUCCESS;
  40. }
  41. if (platform != NULL && platform != &socl_platform)
  42. return CL_INVALID_PLATFORM;
  43. if ((devices != NULL && num_entries == 0)
  44. || (devices == NULL && num_devices == NULL))
  45. return CL_INVALID_VALUE;
  46. if (!(device_type & (CL_DEVICE_TYPE_CPU | CL_DEVICE_TYPE_GPU | CL_DEVICE_TYPE_ACCELERATOR | CL_DEVICE_TYPE_DEFAULT))
  47. && (device_type != CL_DEVICE_TYPE_ALL))
  48. return CL_INVALID_DEVICE_TYPE;
  49. int ndevs = starpu_worker_get_count_by_type(STARPU_OPENCL_WORKER);
  50. int workers[ndevs];
  51. starpu_worker_get_ids_by_type(STARPU_OPENCL_WORKER, workers, ndevs);
  52. if (socl_devices == NULL) {
  53. socl_device_count = ndevs;
  54. socl_devices = malloc(sizeof(struct _cl_device_id) * ndevs);
  55. int i;
  56. for (i=0; i < ndevs; i++) {
  57. int devid = starpu_worker_get_devid(workers[i]);
  58. socl_devices[i].dispatch = &socl_master_dispatch;
  59. socl_devices[i].worker_id = workers[i];
  60. socl_devices[i].device_id = devid;
  61. }
  62. }
  63. int i;
  64. unsigned int num = 0;
  65. for (i=0; i < ndevs; i++) {
  66. int devid = socl_devices[i].device_id;
  67. cl_device_id dev;
  68. starpu_opencl_get_device(devid, &dev);
  69. cl_device_type typ;
  70. clGetDeviceInfo(dev, CL_DEVICE_TYPE, sizeof(typ), &typ, NULL);
  71. if (typ & device_type) {
  72. if (devices != NULL && num < num_entries) devices[num] = &socl_devices[i];
  73. num++;
  74. }
  75. }
  76. if (num_devices != NULL)
  77. *num_devices = num;
  78. return CL_SUCCESS;
  79. }