worker_tree_example.c 2.8 KB

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