MIDAPACK - MIcrowave Data Analysis PACKage  1.1b
Parallel software tools for high performance CMB DA analysis
cindex.c
Go to the documentation of this file.
1 
22 #include <stdlib.h>
23 
24 /* Prototype binary search function */
25 int dichotomy(int nT, int *T, int e);
26 
34 int sindex(int *T, int nT, int *A, int nA) {
35  int i, tmp;
36  i = 0;
37  for (i = 0; i < nA; i++) {
38  tmp = A[i];
39  A[i] = dichotomy(nT, T, tmp);
40  }
41  return 0;
42 }
43 
44 
45 #ifdef W_OPENMP
46 #include <omp.h>
54 int omp_pindex(int *T, int nT, int *A, int nA) {
55  // printf("\nomp_pindex");
56  int i;
57  int *count, *disp;
58  int q, r;
59  int tid, nths;
60 
61 #pragma omp parallel private(tid) shared(nths)
62  { //---fork---just to get the number of threads
63  nths = omp_get_num_threads();
64  tid = omp_get_thread_num();
65  // printf("\ntid %d nths %d", tid, nths);
66  } //---join---
67 
68  q = nA / nths;
69  r = nA % nths;
70 
71  count = (int *) malloc(nths * sizeof(int));
72  disp = (int *) malloc(nths * sizeof(int));
73 
74  for (i = 0; i < nths; i++) {
75  if (i < r) {
76  count[i] = q + 1;
77  } else {
78  count[i] = q;
79  }
80  }
81 
82  disp[0] = 0;
83  for (i = 0; i < nths - 1; i++) { disp[i + 1] = disp[i] + count[i]; }
84 
85 #pragma omp parallel private(tid) shared(T, nT, A, disp, count)
86  { //---fork---1st step, sort on local chunk
87  tid = omp_get_thread_num();
88  sindex(T, nT, A + disp[tid], count[tid]);
89  } //---join---
90  free(count);
91  free(disp);
92  return 0;
93 }
94 #endif
95 
96 
102 int dichotomy(int nT, int *T, int e) {
103  int min, max, pivot;
104  min = 0;
105  max = nT - 1;
106  pivot = (max - min) / 2;
107  while (e != T[pivot] && max > min) {
108  if (T[pivot] < e) {
109  min = pivot + 1;
110  } else {
111  max = pivot;
112  }
113  pivot = min + (max - min) / 2;
114  }
115  return pivot;
116 }
int sindex(int *T, int nT, int *A, int nA)
Definition: cindex.c:34
int omp_pindex(int *T, int nT, int *A, int nA)
Definition: cindex.c:54
int dichotomy(int nT, int *T, int e)
Definition: cindex.c:102