4 /* Steps for the new prefetch call */
5 // Function for new prefetch call
6 void rangePrefetch(int prefetchsiteid, int ntuples, unsigned int *baseoids,
7 unsigned short *numoffsets, short *offsets) {
8 // a[0][1] - a[3][1] = a.0.3
10 // a.f.next.h = a.f.0.next.0.h
11 // a.f.next.next.h = a.f.next.2.h
12 /* Allocate memory in prefetch queue and push the block there */
13 int qnodesize = 2*sizeof(int)+ ntuples *(sizeof(unsigned int) + sizeof(unsigned short)) + numoffsets[ntuples -1] * sizeof(short);
14 char *node = (char *) getmemory(qnodesize);
15 int top = numoffsets[ntuples -1];
21 *((int *)(node)) = prefetchsiteid;
22 *((int *)(node + sizeof(int))) = ntuples;
23 index = 2 * sizeof(int);
24 memcpy(node+index, baseoids, ntuples * sizeof(unsigned int));
25 index = index + ntuples *(sizeof(unsigned int));
26 memcpy(node+index, numoffsets, ntuples * sizeof(unsigned short));
27 index = index + ntuples *(sizeof(unsigned short));
28 memcpy(node+index, offsets, top * sizeof(short));
33 void *transPrefetchNew() {
35 /* Read from prefetch queue */
36 void *node = gettail();
37 /* Check tuples if they are found locally */
43 int getsize(short *ptr, int n) {
44 int sum = 0, newsum, i;
45 for (i = n-1; i >= 0; i--) {
46 newsum = (1 + ptr[i])+((1 + ptr[i])*sum);
52 void checkIfLocal(char *ptr) {
53 unsigned int *baseoids = GET_PTR_OID(ptr);
54 unsigned int ntuples = *(GET_NTUPLES(ptr));
55 unsigned short *endoffsets = GET_PTR_EOFF(ptr, ntuples);
56 short *offsets = GET_PTR_ARRYFLD(ptr, ntuples);
59 // Iterate for each object
60 for (i = 0; i < ntuples; i++) {
61 int numoffset = (i == 0) ? endoffsets[0] : (endoffsets[i] - endoffsets[i-1]);
62 int sizetmpObjSet = numoffset >> 1;
63 unsigned short tmpobjset[sizetmpObjSet];
65 for (l = 0; l < sizetmpObjSet; l++) {
66 tmpobjset[l] = GET_RANGE(offsets[2*l+1]);
68 int maxChldOids = getsize(tmpobjset, sizetmpObjSet)+1;
69 unsigned int chldOffstFrmBase[maxChldOids];
70 chldOffstFrmBase[0] = baseoids[i];
71 int tovisit = 0, visited = -1;
72 // Iterate for each element of offsets
73 for (j = 0; j < numoffset; j++) {
74 // Iterate over each element to be visited
75 while (visited != tovisit) {
76 if (!isOidAvail(chldOffstFrmBase[visited+1])) {
77 //1. Add oid and remaining part to prefetch
79 // iterate over each offset
81 if((retval = lookForObjs(chldOffstFrmBase, offsets, &j,
82 &visited, &tovisit)) == 0) {
83 printf("%s() Error: Object not found %s at line %d\n",
84 __func__, __FILE__, __LINE__);
88 // if new objects to compute
89 // chldOffstFrmBase[++tovisit] = new object oid
93 } // end iterate for each element of offsets
94 } // end iterate for each object
97 int isOidAvail(unsigned int oid) {
99 if((header=(objheader_t *)mhashSearch(oid))!=NULL) {
102 } else if ((header=(objheader_t *)prehashSearch(oid))!=NULL) {
109 int lookForObjs(int *chldOffstFrmBase, short *offsets,
110 int *index, int *visited, int *tovisit) {
112 unsigned int oid = chldOffstFrmBase[*visited+1];
113 if((header = (objheader_t *)mhashSearch(oid))!= NULL) {
116 } else if((header = (objheader_t *)prehashSearch(oid))!=NULL) {
117 //Found in prefetch cache
120 printf("DEBUG->%s()THIS SHOULD NOR HAPPEN\n", __func__);
124 if(TYPE(header) > NUMCLASSES) {
125 int elementsize = classsize[TYPE(header)];
126 struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
127 int length = ao->___length___;
128 /* Check if array out of bounds */
129 int start = offsets[*index];
130 int range = GET_RANGE(offsets[(*index)+1]);
131 short stride = GET_STRIDE(offsets[(*index)+1]);
132 stride = stride + 1; //NOTE bit pattern 000 => stride = 1, 001 => stride = 2
134 for(i = start; ; i= i+stride) {
135 if(offsets[i]>=length || offsets[i] < 0) {
136 chldOffstFrmBase[*tovisit] = 0;
139 //if yes treat the object as found
141 oid = *((unsigned int *)(((char *)ao) +
142 sizeof(struct ArrayObject) + (elementsize*i)));
144 //TODO check is stride +ve or negative
150 } else { //linked list
152 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + *index));
156 // compute new object
158 // if new objects to compute
159 // chldOffstFrmBase[++tovisit] = new object oid
163 int lookForObjs(unsigned int *oid, short *offset, int *numoids, int *newbase) {
165 if((header = mhashSearch(*oid))!= NULL) {
168 } else if((header = prehashSearch(*oid))!=NULL) {
169 //Found in prefetch cache
175 if(TYPE(header) > NUMCLASSES) {
176 int elementsize = classsize[TYPE(header)];
177 struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
178 int length = ao->___length___;
179 /* Check if array out of bounds */
180 if(offset[*newbase] < 0 || offset[*newbase] >= length) {
181 //if yes treat the object as found
185 if(getOtherOid(header, ao, offset, numoids, newbase))
188 } else { //linked list
189 //(*oid) = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offset));
190 if(getNext(header, offset, numoids, newbase))
196 void resolveArrays(unsigned int *arrayOfOids, short *offset, int *numoids, int *newbase) {
202 int getOtherOid(header, ao, offset, numoids, newbase) {
204 short startindex = offset[*newbase];
205 int getnewbaseVal = *newbase + 1;
206 if(getnewbaseVal == 0) {
207 (*oid) = *((unsigned int *)(((char *)ao) + sizeof(struct ArrayObject) + (elementsize*startindex)));
208 (*newbase) = (*newbase) + 2; //skip the immediate offset
210 } else if(getnewbaseVal > 0) {
211 /* Resolve the oids within a given range */
213 range = GET_RANGE(offset[*newbase]);
214 stride = GET_STRIDE((void *)(offset[*newbase]));
215 stride = stride + 1; //NOTE 000 => stride = 1, 001 => stride = 2
217 unsigned int arrayOfOids[range+1];
218 if(GET_STRIDEINC(offset[*newbase])) { //-ve stride
220 for(i = startindex; i <= range; i = i - stride) {
221 if(i < 0 || i >= length) {
222 //if yes treat the object as found
226 arrayOfOids[index] = *((unsigned int *)(((char *)ao) + sizeof(struct ArrayObject) + (elementsize*i)));
229 } else { //+ve stride
231 for(i = startindex; i <= range; i = i + stride) {
232 if(i < 0 || i >= length) {
233 //if yes treat the object as found
237 arrayOfOids[index] = *((unsigned int *)(((char *)ao) + sizeof(struct ArrayObject) + (elementsize*i)));
241 //(*oid) = *((unsigned int *)(((char *)ao) + sizeof(struct ArrayObject) + (elementsize*startindex)));
242 (*newbase) = (*newbase) + 2;
252 void checkIfLocal(char *ptr) {
253 int siteid = *(GET_SITEID(ptr));
254 int ntuples = *(GET_NTUPLES(ptr));
255 unsigned int *baseoids = GET_PTR_OID(ptr);
256 unsigned short *numoffsets = GET_PTR_EOFF(ptr, ntuples);
257 short *offsets = GET_PTR_ARRYFLD(ptr, ntuples);
258 prefetchpile_t * head=NULL;
262 for(i=0; i<ntuples; i++) {
263 unsigned short baseindex=(i==0) ? 0 : numoffsets[i -1];
264 unsigned short endindex = numoffsets[i];
265 unsigned int oid = baseoids[i];
270 //Look up fields locally
272 for(newbase=baseindex; newbase<endindex; ) {
273 if(!lookForObjs(&oid, &offsets[newbase], &numoids, &newbase)) {
276 //Ended in a null pointer
281 //Add to remote request
282 machinenum=lhashSearch(oid);
283 // Create an array of oids and offsets
284 unsigned int arrayoid[numoids];
285 unsigned short arraynumoffset[numoids];
286 void *arryfields[numoids];
287 for(i = 0; i<numoids; i++) {
289 arraynumoffset[i] = endindex - newbase;
290 arryfields[i] = (void*)(&arryfields[newbase]);
292 //insertPile(machinenum, oid, endindex-newbase, &arryfields[newbase], &head);
293 insertPile(machinenum, arrayoid, arraynumoffset, arryfields, &head);