fork download
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <time.h>
  6. #include <omp.h>
  7.  
  8. void down_sweep(int *a, int n) {
  9. a[n - 1] = 0; // Set the identity
  10.  
  11. for (int d = (int)log2(n) - 1; d >= 0; --d) {
  12. #pragma omp parallel for
  13. for (int i = 0; i <= n - (1 << (d+1)); i += (1 << (d+1))) {
  14. int t = a[i + (1 << d) - 1];
  15. a[i + (1 << d) - 1] = a[i + (1 << (d+1)) - 1];
  16. a[i + (1 << (d+1)) - 1] += t;
  17. }
  18. }
  19. }
  20. int main() {
  21. int input_size = 1000000;
  22. int next_power_of_two = (int)pow(2, ceil(log2(input_size)));
  23. int *input = malloc(next_power_of_two * sizeof(int));
  24. if (input == NULL) {
  25. return EXIT_FAILURE;
  26. }
  27.  
  28. for (int i = 0; i < next_power_of_two; i++) {
  29. input[i] = 3;
  30. }
  31.  
  32. struct timespec start_time, end_time;
  33. clock_gettime(CLOCK_MONOTONIC, &start_time);
  34.  
  35. down_sweep(input, next_power_of_two);
  36.  
  37. clock_gettime(CLOCK_MONOTONIC, &end_time);
  38. double seconds = (end_time.tv_sec - start_time.tv_sec) +
  39. (end_time.tv_nsec - start_time.tv_nsec) / 1e9;
  40. double operations = (double)(next_power_of_two - 1); // Number of operations
  41. double gdlops = (operations / seconds) / 1e9;
  42.  
  43. printf("Time taken: %f seconds\n", seconds);
  44. printf("GDLOPS: %f\n", gdlops);
  45.  
  46. free(input);
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0.01s 5644KB
stdin
Standard input is empty
stdout
Time taken: 0.002015 seconds
GDLOPS: 0.520461