bug fixes
[IRC.git] / Robust / src / Runtime / bamboo / multicoremem.c
1 #ifdef MULTICORE
2 #include "runtime_arch.h"
3 #include "multicoreruntime.h"
4
5 #ifdef MULTICORE_GC
6 #include "multicoregarbage.h"
7 #include "multicorehelper.h"
8 #include "multicoremem_helper.h"
9
10 // Only allocate local mem chunks to each core.
11 // If a core has used up its local shared memory, start gc.
12 void * localmalloc_I(int coren,
13                      unsigned int memcheck,
14                      int * allocsize) {
15   for(block_t localblocknum=0;localblocknum<GCNUMLOCALBLOCK;localblocknum++) {
16     block_t searchblock=BLOCKINDEX2(coren, localblocknum);
17     struct blockrecord * block=&allocationinfo.blocktable[searchblock];
18     if (block->status==BS_FREE) {
19       unsigned INTPTR freespace=block->freespace&~BAMBOO_CACHE_LINE_MASK;
20       if (freespace>=memcheck) {
21         //we have a block
22         //mark block as used
23         block->status=BS_USED;
24         void *blockptr=OFFSET2BASEVA(searchblock)+gcbaseva;
25         unsigned INTPTR usedspace=((block->usedspace-1)&~BAMBOO_CACHE_LINE_MASK)+BAMBOO_CACHE_LINE_SIZE;
26         void *startaddr=blockptr+usedspace;
27         *allocsize=freespace;
28         return startaddr;
29       }
30     }
31   }
32   
33   return NULL;
34
35
36 // Allocate the local shared memory to each core with the highest priority,
37 // if a core has used up its local shared memory, try to allocate the 
38 // shared memory that belong to its neighbours, if also failed, start gc.
39 void * fixedmalloc_I(int coren,
40                      unsigned int memcheck,
41                      int * allocsize) {
42   //try locally first
43   void * mem=localmalloc_I(coren,memcheck,allocsize);
44   if (mem!=NULL)
45     return mem;
46
47   //failed try neighbors...in a round robin fashion
48   
49   for(block_t lblock=0;lblock<MAXNEIGHBORALLOC;lblock++) {  
50     for(int i=0;i<NUM_CORES2TEST;i++) {
51       int neighborcore=core2test[corenum][i];
52       if (neighborcore!=-1) {
53         block_t globalblockindex=BLOCKINDEX2(neighborcore, lblock);
54         struct blockrecord * block=&allocationinfo.blocktable[globalblockindex];
55         if (block->status==BS_FREE) {
56           unsigned INTPTR freespace=block->freespace&~BAMBOO_CACHE_LINE_MASK;
57           if (memcheck<=freespace) {
58             //we have a block
59             //mark block as used
60             block->status=BS_USED;
61             void *blockptr=OFFSET2BASEVA(globalblockindex)+gcbaseva;
62             unsigned INTPTR usedspace=((block->usedspace-1)&~BAMBOO_CACHE_LINE_MASK)+BAMBOO_CACHE_LINE_SIZE;
63             *allocsize=usedspace;
64             return blockptr+usedspace;
65           }
66         }
67       }
68     }
69   }
70
71   //no memory
72   return NULL;
73
74
75
76 // Allocate the local shared memory to each core with the highest priority,
77 // if a core has used up its local shared memory, try to allocate the 
78 // shared memory that belong to its neighbours first, if failed, check 
79 // current memory allocation rate, if it has already reached the threshold,
80 // start gc, otherwise, allocate the shared memory globally.  If all the 
81 // shared memory has been used up, start gc.
82 void * mixedmalloc_I(int coren,
83                      int isize,
84                      int * allocsize) {
85   void * mem=fixedmalloc_I(coren,isize,allocsize);
86   if (mem!=NULL)
87     return mem;
88
89   //try global allocator instead
90   return globalmalloc_I(coren, isize, allocsite);
91
92
93
94 // Allocate all the memory chunks globally, do not consider the host cores
95 // When all the shared memory are used up, start gc.
96 void * globalmalloc_I(int coren, unsigned INTPTR memcheck, int * allocsize) {
97   block_t firstfree=NOFREEBLOCK;
98   block_t lowestblock=allocationinfo.lowestfreeblock;
99
100   for(block_t searchblock=lowestblock;searchblock<GCNUMBLOCK;searchblock++) {
101     struct blockrecord * block=&allocationinfo.blocktable[searchblock];
102     if (block->status==BS_FREE) {
103       if(firstfree==NOFREEBLOCK)
104         firstfree=searchblock;
105       unsigned INTPTR freespace=block->freespace&~BAMBOO_CACHE_LINE_MASK;
106       if (freespace>=memcheck) {
107         //we have a block
108         //mark block as used
109         block->status=BS_USED;
110         void *blockptr=OFFSET2BASEVA(searchblock)+gcbaseva;
111         unsigned INTPTR usedspace=((block->usedspace-1)&~BAMBOO_CACHE_LINE_MASK)+BAMBOO_CACHE_LINE_SIZE;
112         allocationinfo.lowestfreeblock=firstfree;
113         void *startaddr=blockptr+usedspace;
114         *allocsize=freespace;
115         return startaddr;
116       }
117     }
118   }
119   return NULL;
120
121
122 void * smemalloc(int coren, int isize, int * allocsize) {
123   BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
124   void *retval=smemalloc_I(coren, isize, allocsize);
125   BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
126   return retval;
127 }
128
129 // malloc from the shared memory
130 void * smemalloc_I(int coren, int isize, int * allocsize) {
131 #ifdef SMEML
132   void *mem = localmalloc_I(coren, isize, allocsize);
133 #elif defined(SMEMF)
134   void *mem = fixedmalloc_I(coren, isize, allocsize);
135 #elif defined(SMEMM)
136   void *mem = mixedmalloc_I(coren, isize, allocsize);
137 #elif defined(SMEMG)
138   void *mem = globalmalloc_I(coren, isize, allocsize);
139 #endif
140
141   if(mem == NULL) {
142     // not enough shared global memory
143     // trigger gc
144     if(!gcflag) {
145       gcflag = true;
146       if(!gc_status_info.gcprocessing) {
147         // inform other cores to stop and wait for gc
148         gcprecheck = true;
149         for(int i = 0; i < NUMCORESACTIVE; i++) {
150           // reuse the gcnumsendobjs & gcnumreceiveobjs
151           gcnumsendobjs[0][i] = 0;
152           gcnumreceiveobjs[0][i] = 0;
153         }
154         GC_SEND_MSG_1_TO_CLIENT(GCSTARTPRE);
155       }
156     }
157     return NULL;
158   }
159   return mem;
160 }
161 #else
162 // malloc from the shared memory
163 void * smemalloc_I(int coren,
164                    int size,
165                    int * allocsize) {
166   void * mem = NULL;
167   int toallocate = (size>(BAMBOO_SMEM_SIZE)) ? (size) : (BAMBOO_SMEM_SIZE);
168   if(toallocate > bamboo_free_smem_size) {
169     // no enough mem
170     mem = NULL;
171   } else {
172     mem = (void *)bamboo_free_smemp;
173     bamboo_free_smemp = ((void*)bamboo_free_smemp) + toallocate;
174     bamboo_free_smem_size -= toallocate;
175   }
176   *allocsize = toallocate;
177   if(mem == NULL) {
178     // no enough shared global memory
179     *allocsize = 0;
180     BAMBOO_EXIT();
181   }
182   return mem;
183
184 #endif // MULTICORE_GC
185
186 #endif // MULTICORE