forked from parallella/pal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp_mode.c
48 lines (43 loc) · 1.14 KB
/
p_mode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <pal.h>
/**
*
* Calculates the mode value of input vector 'a'.
*
* @param a Pointer to input vector
*
* @param c Pointer to output scalar
*
* @param n Size of 'a' vector.
*
* @param p Number of processor to use (task parallelism)
*
* @param team Team to work with
*
* @return None
*
*/
void p_mode_f32(const float *a, float *c, int n, int p, p_team_t team)
{
unsigned int occurrence_count = 0;
unsigned int max_occurrence_count = 0;
unsigned int i = 1;
float mode_value = 0.0f;
float *sorted_a = (float*) p_malloc(team, sizeof(float) * n);
p_sort_f32(a, sorted_a, n, p, team);
for (; i < n; ++i) {
++occurrence_count;
if (sorted_a[i] != sorted_a[i - 1]) {
if (occurrence_count > max_occurrence_count) {
max_occurrence_count = occurrence_count;
mode_value = sorted_a[i - 1];
}
occurrence_count = 0;
}
}
if (occurrence_count > max_occurrence_count) {
max_occurrence_count = occurrence_count;
mode_value = sorted_a[n - 1];
}
*c = mode_value;
p_free(sorted_a);
}