vector_scal.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  4. * Copyright (C) 2010-2013 Université de Bordeaux 1
  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 example demonstrates how to use StarPU to scale an array by a factor.
  19. * It shows how to manipulate data with StarPU's data management library.
  20. * 1- how to declare a piece of data to StarPU (starpu_vector_data_register)
  21. * 2- how to describe which data are accessed by a task (task->handles[0])
  22. * 3- how a kernel can manipulate the data (buffers[0].vector.ptr)
  23. */
  24. #include <starpu.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <math.h>
  28. #define NX 204800
  29. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  30. int main(int argc, char **argv)
  31. {
  32. int * A,*B,*C,*D,*E;
  33. putenv("STARPU_LIMIT_CPU_MEM=130");
  34. /* Initialize StarPU with default configuration */
  35. int ret = starpu_init(NULL);
  36. /* register a disk */
  37. unsigned dd = starpu_disk_register(&write_on_file, (void *) "/tmp/", 1024*1024*200);
  38. /* allocate two memory spaces */
  39. starpu_malloc((void **)&A, NX*sizeof(int));
  40. starpu_malloc((void **)&E, NX*sizeof(int));
  41. if (ret == -ENODEV) goto enodev;
  42. FPRINTF(stderr, "Test of disk memory \n");
  43. int j;
  44. for(j = 0; j < NX; ++j)
  45. {
  46. A[j] = j;
  47. E[j] = -j;
  48. }
  49. /* Tell StaPU to associate the "vector" vector with the "vector_handle"
  50. * identifier. When a task needs to access a piece of data, it should
  51. * refer to the handle that is associated to it.
  52. * In the case of the "vector" data interface:
  53. * - the first argument of the registration method is a pointer to the
  54. * handle that should describe the data
  55. * - the second argument is the memory node where the data (ie. "vector")
  56. * resides initially: 0 stands for an address in main memory, as
  57. * opposed to an adress on a GPU for instance.
  58. * - the third argument is the adress of the vector in RAM
  59. * - the fourth argument is the number of elements in the vector
  60. * - the fifth argument is the size of each element.
  61. */
  62. starpu_data_handle_t vector_handleA, vector_handleB, vector_handleC, vector_handleD, vector_handleE;
  63. starpu_vector_data_register(&vector_handleA, 0, (uintptr_t)A, NX, sizeof(int));
  64. starpu_vector_data_register(&vector_handleB, -1, (uintptr_t) NULL, NX, sizeof(int));
  65. starpu_vector_data_register(&vector_handleC, -1, (uintptr_t) NULL, NX, sizeof(int));
  66. starpu_vector_data_register(&vector_handleD, -1, (uintptr_t) NULL, NX, sizeof(int));
  67. starpu_vector_data_register(&vector_handleE, 0, (uintptr_t)E, NX, sizeof(int));
  68. starpu_data_cpy(vector_handleB, vector_handleA, 1, NULL, NULL);
  69. starpu_data_cpy(vector_handleC, vector_handleB, 1, NULL, NULL);
  70. starpu_data_cpy(vector_handleD, vector_handleC, 1, NULL, NULL);
  71. starpu_data_cpy(vector_handleE, vector_handleD, 1, NULL, NULL);
  72. /* StarPU does not need to manipulate the array anymore so we can stop
  73. * monitoring it */
  74. starpu_data_unregister(vector_handleA);
  75. starpu_data_unregister(vector_handleB);
  76. starpu_data_unregister(vector_handleC);
  77. starpu_data_unregister(vector_handleD);
  78. starpu_data_unregister(vector_handleE);
  79. starpu_disk_unregister(dd);
  80. /* terminate StarPU, no task can be submitted after */
  81. starpu_shutdown();
  82. int try = 1;
  83. for (j = 0; j < NX; ++j)
  84. if (A[j] != E[j])
  85. {
  86. printf("fail A %d != E %d \n", A[j], E[j]);
  87. try = 0;
  88. }
  89. if(try)
  90. FPRINTF(stderr, "TEST SUCCESS\n");
  91. else
  92. FPRINTF(stderr, "TEST FAIL\n");
  93. return (try ? EXIT_SUCCESS : EXIT_FAILURE);
  94. enodev:
  95. return 77;
  96. }