worker_tree_example.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2016 Université de Bordeaux
  4. * Copyright (C) 2010-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 trees.
  19. */
  20. #include <starpu.h>
  21. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  22. #if !defined(STARPU_HAVE_HWLOC)
  23. #warning hwloc is not enabled. Skipping test
  24. int main(int argc, char **argv)
  25. {
  26. return 77;
  27. }
  28. #else
  29. int main()
  30. {
  31. int ret;
  32. ret = starpu_init(NULL);
  33. if (ret == -ENODEV)
  34. return 77;
  35. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  36. int procs[STARPU_NMAXWORKERS];
  37. unsigned ncpus = starpu_cpu_worker_get_count();
  38. starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, procs, ncpus);
  39. struct starpu_worker_collection *co = (struct starpu_worker_collection*)calloc(1, sizeof(struct starpu_worker_collection));
  40. co->has_next = worker_tree.has_next;
  41. co->get_next = worker_tree.get_next;
  42. co->add = worker_tree.add;
  43. co->remove = worker_tree.remove;
  44. co->init = worker_tree.init;
  45. co->deinit = worker_tree.deinit;
  46. co->init_iterator = worker_tree.init_iterator;
  47. co->type = STARPU_WORKER_TREE;
  48. FPRINTF(stderr, "ncpus %u \n", ncpus);
  49. double start_time;
  50. double end_time;
  51. start_time = starpu_timing_now();
  52. co->init(co);
  53. end_time = starpu_timing_now();
  54. double timing = (end_time - start_time) / 1000;
  55. unsigned i;
  56. for(i = 0; i < ncpus; i++)
  57. {
  58. int added = co->add(co, procs[i]);
  59. FPRINTF(stderr, "added proc %d to the tree \n", added);
  60. }
  61. struct starpu_sched_ctx_iterator it;
  62. int pu;
  63. co->init_iterator(co, &it);
  64. while(co->has_next(co, &it))
  65. {
  66. pu = co->get_next(co, &it);
  67. FPRINTF(stderr, "pu = %d out of %u workers \n", pu, co->nworkers);
  68. }
  69. unsigned six = 6;
  70. if (six < ncpus)
  71. six = ncpus/2;
  72. for(i = 0; i < six; i++)
  73. {
  74. co->remove(co, i);
  75. FPRINTF(stderr, "remove %u out of %u workers\n", i, co->nworkers);
  76. }
  77. while(co->has_next(co, &it))
  78. {
  79. pu = co->get_next(co, &it);
  80. FPRINTF(stderr, "pu = %d out of %u workers \n", pu, co->nworkers);
  81. }
  82. FPRINTF(stderr, "timing init = %lf \n", timing);
  83. co->deinit(co);
  84. starpu_shutdown();
  85. free(co);
  86. return 0;
  87. }
  88. #endif