cl_getdeviceids.c 2.7 KB

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