worker_list_example.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2015 Université de Bordeaux
  4. * Copyright (C) 2010-2014, 2016 CNRS
  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. /*
  18. * This shows how to manipulate worker lists.
  19. */
  20. #include <starpu.h>
  21. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  22. int main()
  23. {
  24. int ret;
  25. ret = starpu_init(NULL);
  26. if (ret == -ENODEV)
  27. return 77;
  28. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  29. int procs[STARPU_NMAXWORKERS];
  30. unsigned ncpus = starpu_cpu_worker_get_count();
  31. starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, procs, ncpus);
  32. struct starpu_worker_collection *co = (struct starpu_worker_collection*)malloc(sizeof(struct starpu_worker_collection));
  33. co->has_next = worker_list.has_next;
  34. co->get_next = worker_list.get_next;
  35. co->add = worker_list.add;
  36. co->remove = worker_list.remove;
  37. co->init = worker_list.init;
  38. co->deinit = worker_list.deinit;
  39. co->init_iterator = worker_list.init_iterator;
  40. co->type = STARPU_WORKER_LIST;
  41. FPRINTF(stderr, "ncpus %u\n", ncpus);
  42. double start_time;
  43. double end_time;
  44. start_time = starpu_timing_now();
  45. co->init(co);
  46. end_time = starpu_timing_now();
  47. double timing = (end_time - start_time) / 1000;
  48. unsigned i;
  49. for(i = 0; i < ncpus; i++)
  50. {
  51. int added = co->add(co, procs[i]);
  52. FPRINTF(stderr, "added proc %d to the tree \n", added);
  53. }
  54. struct starpu_sched_ctx_iterator it;
  55. int pu;
  56. co->init_iterator(co, &it);
  57. while(co->has_next(co, &it))
  58. {
  59. pu = co->get_next(co, &it);
  60. FPRINTF(stderr, "pu = %d out of %u workers \n", pu, co->nworkers);
  61. }
  62. for(i = 0; i < 6; i++)
  63. {
  64. co->remove(co, i);
  65. FPRINTF(stderr, "remove %u out of %u workers\n", i, co->nworkers);
  66. }
  67. while(co->has_next(co, &it))
  68. {
  69. pu = co->get_next(co, &it);
  70. FPRINTF(stderr, "pu = %d out of %u workers\n", pu, co->nworkers);
  71. }
  72. FPRINTF(stderr, "timing init = %lf \n", timing);
  73. co->deinit(co);
  74. free(co);
  75. starpu_shutdown();
  76. return 0;
  77. }