worker_list_example.c 2.6 KB

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