Advertisement
Guest User

Philipp

a guest
Dec 7th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. /* ------------------------------------------------------ */
  2. /* Serielles Program zum Finden des groessten Elementes */
  3. /* in einem Integer-Vektor */
  4. /* ------------------------------------------------------ */
  5.  
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <omp.h>
  9. #define VEC_DIM 20
  10.  
  11. int total_checksum=0;
  12.  
  13. /* fill array with random numbers */
  14. void init_vector(int *vec, int n, char *dat){
  15.  
  16. int i;
  17. FILE *in;
  18.  
  19. in = fopen(dat, "r");
  20.  
  21. if (in == NULL)
  22. printf("ERROR: Cannot open file %s!\n",dat);
  23. else{
  24.  
  25. for(i=0; i<n; i++)
  26. fscanf(in,"%d",&vec[i]);
  27.  
  28. fclose(in);
  29. }
  30. }
  31.  
  32. int calc_checksum(int *vec, int n){
  33.  
  34. int sum=0;
  35. int i;
  36.  
  37. for(i=0; i<n; i++)
  38. sum += vec[i];
  39.  
  40. total_checksum += sum;
  41.  
  42. return sum;
  43. }
  44.  
  45. int main (int argc, char **argv){
  46.  
  47. int i, j;
  48. int x=11;
  49. int tmp;
  50. int max_val, max_ind;
  51. int va[VEC_DIM];
  52. int vb[VEC_DIM];
  53. int vc[VEC_DIM];
  54. int vd[VEC_DIM];
  55. int ve[VEC_DIM];
  56. int vf[VEC_DIM];
  57. int check_va, check_vb, check_ve, check_vf;
  58.  
  59. /* initialize arrays */
  60. #pragma omp parallel sections
  61. {
  62. #pragma omp section
  63. {
  64. init_vector(va, VEC_DIM, "input_va.dat");
  65. for(i=0; i<VEC_DIM; ++i)
  66. va[i] += x;
  67. check_va = calc_checksum(va, VEC_DIM);
  68. printf("%d --> Checksum of vector a: %d\n",omp_get_thread_num(), check_va);
  69. }
  70. #pragma omp section
  71. {
  72. init_vector(vb, VEC_DIM, "input_vb.dat");
  73. for(i=0; i<VEC_DIM/2; ++i){
  74. tmp = vb[i];
  75. vb[i] = vb[VEC_DIM-1-i];
  76. vb[VEC_DIM-1-i] = tmp;
  77. }
  78. check_vb = calc_checksum(vb, VEC_DIM);
  79. printf("%d --> Checksum of vector b: %d\n",omp_get_thread_num(),check_vb);
  80.  
  81. }
  82.  
  83. #pragma omp section
  84. {
  85. init_vector(vc, VEC_DIM, "input_vc.dat");
  86. init_vector(vd, VEC_DIM, "input_vd.dat");
  87. for(i=0; i<VEC_DIM; ++i) {
  88. ve[i] = vc[i] + vd[i];
  89. }
  90. check_ve = calc_checksum(ve, VEC_DIM);
  91. printf("%d --> Checksum of vector e: %d\n",omp_get_thread_num(),check_ve);
  92.  
  93. }
  94.  
  95. #pragma omp section
  96. {
  97. init_vector(vf, VEC_DIM, "input_vf.dat");
  98. for(i=0; i<VEC_DIM; ++i){
  99. max_val = vf[i];
  100. max_ind = i;
  101. for(j=i; j<VEC_DIM; ++j){
  102. if(vf[j] > max_val){
  103. max_val = vf[j];
  104. max_ind = i;
  105. }
  106. }
  107. tmp = vf[i];
  108. vf[i] = vf[max_ind];
  109. vf[max_ind] = tmp;
  110. }
  111. check_vf = calc_checksum(vf, VEC_DIM);
  112. printf("%d --> Checksum of vector f: %d\n",omp_get_thread_num(),check_vf);
  113.  
  114. }
  115. }
  116.  
  117. printf("Total checksum: %d\n",total_checksum);
  118.  
  119. return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement