PAPI 7.1.0.0
Loading...
Searching...
No Matches
prepareArray.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <stdlib.h>
3#include <inttypes.h>
4#include <unistd.h>
5#include <assert.h>
6
7#include "prepareArray.h"
8
9volatile uintptr_t opt_killer_zero;
10static void _prepareArray_sections_random(uintptr_t *array, long long len, long long stride, long long secSize);
11static void _prepareArray_sequential(uintptr_t *array, long long len, long long stride);
12
13
14/*
15 * "stride" is in "uintptr_t" elements, NOT in bytes
16 * Note: It is wise to provide an "array" that is aligned to the cache line size.
17 */
18int prepareArray(uintptr_t *array, long long len, long long stride, long long secSize, int pattern){
19 assert( array != NULL );
20 opt_killer_zero = (uintptr_t)( (len+37)/(len+36) - 1 );
21
22 switch(pattern){
23 case SECRND:
24 _prepareArray_sections_random(array, len, stride, secSize);
25 break;
26 case SEQUEN:
27 _prepareArray_sequential(array, len, stride);
28 break;
29 default:
30 fprintf(stderr,"prepareArray() unknown array access pattern: %d\n",pattern);
31 return -1;
32 break;
33 }
34 return 0;
35}
36
37/*
38 * "stride" is in "uintptr_t" elements, NOT in bytes
39 * Note: It is wise to provide an "array" that is aligned to the cache line size.
40 */
41static void _prepareArray_sections_random(uintptr_t *array, long long len, long long stride, long long secSize){
42
43 assert( array != NULL );
44
45 long long elemCnt, maxElemCnt, sec, i;
46 long long currElemCnt, uniqIndex, taken;
47 uintptr_t **p, *next;
48 long long currSecSize = secSize;
49 long long secCnt = 1+len/secSize;
50 long long *availableNumbers;
51
52 p = (uintptr_t **)&array[0];
53
54 maxElemCnt = currSecSize/stride;
55 availableNumbers = (long long *)calloc(maxElemCnt, sizeof(long long));
56
57 // Loop through every section in the array.
58 for(sec=0; sec<secCnt; ++sec){
59
60 // If we are at the last section, trim the size in order to link back to the first section.
61 if( sec == secCnt-1 )
62 currSecSize = (len%secSize);
63
64 for(i=0; i<maxElemCnt; i++)
65 availableNumbers[i] = i;
66
67 currElemCnt = currSecSize/stride;
68
69 taken = 0;
70
71 // For the first section, we have already picked "0", so we must pick one less element.
72 if( 0==sec )
73 taken = 1;
74 long long remainingElemCnt = currElemCnt;
75
76 for(elemCnt=0; elemCnt<currElemCnt-taken; ++elemCnt){
77 // Skip the first "taken" elements.
78 long long index = taken + random() % (remainingElemCnt-taken);
79 // For the first section, skip zero as a choice (we already selected it before the loop).
80 uniqIndex = sec*secSize + stride*availableNumbers[index];
81 // Replace the chosen number with the last element.
82 availableNumbers[index] = availableNumbers[remainingElemCnt-1];
83 // Shrink the effective array size so the last element "drops off."
84 remainingElemCnt--;
85
86 // Connect the link.
87 next = &array[uniqIndex];
88 *p = next;
89 p = (uintptr_t **)next;
90 }
91 }
92
93 // Close the circle by pointing the last element to the start.
94 next = &array[0];
95 *p = next;
96
97 free(availableNumbers);
98
99 return;
100}
101
102/*
103 * "stride" is in "uintptr_t" elements, NOT in bytes
104 * Note: It is wise to provide an "array" that is aligned to the cache line size.
105 */
106static void _prepareArray_sequential(uintptr_t *array, long long len, long long stride){
107 long long curr;
108 uintptr_t **p, *next;
109
110 p = (uintptr_t **)&array[0];
111
112 // Point each element to the next in the array.
113 for(curr=0; curr<len; curr+=stride){
114 next = &array[curr];
115 *p = next;
116 p = (uintptr_t **)next;
117 }
118
119 // Close the circle by pointing the last element to the start.
120 next = &array[0];
121 *p = next;
122
123 return;
124}
125
int i
FILE * stderr
static double array[ARRAYSIZE]
Definition: papi_l1_dca.c:23
volatile uintptr_t opt_killer_zero
Definition: prepareArray.c:9
int prepareArray(uintptr_t *array, long long len, long long stride, long long secSize, int pattern)
Definition: prepareArray.c:18
static void _prepareArray_sections_random(uintptr_t *array, long long len, long long stride, long long secSize)
Definition: prepareArray.c:41
static void _prepareArray_sequential(uintptr_t *array, long long len, long long stride)
Definition: prepareArray.c:106
#define SECRND
Definition: prepareArray.h:7
#define SEQUEN
Definition: prepareArray.h:8