worker_list_example.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. /*
  17. * This shows how to manipulate worker lists.
  18. */
  19. #include <starpu.h>
  20. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  21. int main()
  22. {
  23. int ret;
  24. ret = starpu_init(NULL);
  25. if (ret == -ENODEV)
  26. return 77;
  27. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  28. int procs[STARPU_NMAXWORKERS];
  29. unsigned ncpus = starpu_cpu_worker_get_count();
  30. starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, procs, ncpus);
  31. struct starpu_worker_collection *co = (struct starpu_worker_collection*)malloc(sizeof(struct starpu_worker_collection));
  32. co->has_next = starpu_worker_list.has_next;
  33. co->get_next = starpu_worker_list.get_next;
  34. co->add = starpu_worker_list.add;
  35. co->remove = starpu_worker_list.remove;
  36. co->init = starpu_worker_list.init;
  37. co->deinit = starpu_worker_list.deinit;
  38. co->init_iterator = starpu_worker_list.init_iterator;
  39. co->type = STARPU_WORKER_LIST;
  40. FPRINTF(stderr, "ncpus %u\n", ncpus);
  41. double start_time;
  42. double end_time;
  43. start_time = starpu_timing_now();
  44. co->init(co);
  45. end_time = starpu_timing_now();
  46. double timing = (end_time - start_time) / 1000;
  47. unsigned i;
  48. for(i = 0; i < ncpus; i++)
  49. {
  50. int added = co->add(co, procs[i]);
  51. FPRINTF(stderr, "added proc %d to the tree \n", added);
  52. }
  53. struct starpu_sched_ctx_iterator it;
  54. int pu;
  55. co->init_iterator(co, &it);
  56. while(co->has_next(co, &it))
  57. {
  58. pu = co->get_next(co, &it);
  59. FPRINTF(stderr, "pu = %d out of %u workers \n", pu, co->nworkers);
  60. }
  61. for(i = 0; i < 6; i++)
  62. {
  63. co->remove(co, i);
  64. FPRINTF(stderr, "remove %u out of %u workers\n", i, co->nworkers);
  65. }
  66. while(co->has_next(co, &it))
  67. {
  68. pu = co->get_next(co, &it);
  69. FPRINTF(stderr, "pu = %d out of %u workers\n", pu, co->nworkers);
  70. }
  71. FPRINTF(stderr, "timing init = %lf \n", timing);
  72. co->deinit(co);
  73. free(co);
  74. starpu_shutdown();
  75. return 0;
  76. }