worker_tree_example.c 2.7 KB

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