# rockstar

 ``` 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295``` ```void factor_3(int64_t in, int64_t *factors) { int64_t i, n=0; for (i = ceil(fabs(cbrt(in))); i>0&&n<2; i--) if ((in % i)==0) { in /= i; factors[n++] = i; i = ceil(sqrt(fabs(in)))+1; } factors[2] = in; } //Divide box into equal numbers of particles void divide_projection(struct projection *proj, int64_t pieces, float *places) { int64_t i, np=0, n=1, cp=0, cpp; double f; assert(pieces > 0); for (i=0; idata[i]; np = (np + pieces - 1)/pieces; //Round up, if possible places[0] = 0; for (i=0; idata[i]; if (cp > n*np) { cpp = cp - proj->data[i]; //Calculate fractional location of division f = (cp > cpp) ? ((double)((n*np)-cpp)/(double)(cp-cpp)) : 0; places[n] = BOX_SIZE * (((double)i+f) / (double)PROJECTION_SIZE); n++; } } if (n chunks[1]) SWAP(0,1); if (chunks[0] > chunks[2]) SWAP(0,2); if (chunks[1] > chunks[2]) SWAP(1,2); #undef SWAP } void populate_bounds(int64_t pos, float *oldbounds, float *newbounds, float new_min, float new_max) { for (int64_t i=0; i<6; i++) { if ((i%3)< pos) newbounds[i] = oldbounds[i]; else if ((i%3)==pos) newbounds[i] = (i<3) ? new_min : new_max; else newbounds[i] = (i<3) ? 0 : BOX_SIZE; } } void send_projection_requests(struct projection *pr, int64_t num_requests) { int64_t i, j, num_to_send; float bounds[6]; struct projection_request *prq = NULL; prq = check_realloc(prq, sizeof(struct projection_request)*num_requests, "Allocating projection requests."); for (i=0; i= 0) && (num_to_recv <= max_requests)); for (j=0; j= start) && (cproj.id < (start+num_requests))); for (k=0; k BOX_SIZE) { fprintf(stderr, "[Error] Received invalid format from load balance script!\n"); fprintf(stderr, "[Error] Offending line: %s", buffer); fprintf(stderr, "[Error] Bounds must be within the range 0 to %f\n", BOX_SIZE); exit(1); } memcpy(clients[NUM_READERS+n].bounds, bounds, sizeof(float)*6); send_to_socket_noconfirm(clients[NUM_READERS+n].cs, "bnds", 4); send_to_socket_noconfirm(clients[NUM_READERS+n].cs, clients[NUM_READERS+n].bounds, sizeof(float)*6); } rw_socket_close(script, pid); } void decide_chunks_for_memory_balance() { struct projection *pr = NULL; int64_t num_proj, proj_start, todo, i, j, dir, offset; float *divisions = NULL, *bnds; sort_chunks(); num_proj = 1 + (1 + chunks[1])*chunks[0]; pr = check_realloc(pr, sizeof(struct projection)*num_proj, "Allocating projections."); divisions = check_realloc(divisions, sizeof(float)*(chunks[2]+1), "Allocating box divisions."); for (i=0; i= NUM_READERS && clients[next_assigned].status) next_assigned--; if (next_assigned < no_more_work) next_assigned = num_clients-1; while (next_assigned >= NUM_READERS && clients[next_assigned].status) next_assigned--; if (next_assigned < no_more_work) send_to_socket_noconfirm(clients[i].cs, "fini", 4); else { send_to_socket_noconfirm(clients[i].cs, "work", 4); send_to_socket_noconfirm(clients[i].cs, &next_assigned, sizeof(int64_t)); send_to_socket_noconfirm(clients[i].cs,clients[next_assigned].address, strlen(clients[next_assigned].address)+1); send_to_socket_noconfirm(clients[i].cs, clients[next_assigned].serv_port, strlen(clients[next_assigned].serv_port)+1); clients[next_assigned].extra_workers++; next_assigned--; } } else { fprintf(stderr, "[Error] Client protocol error! (%s) (%"PRId64")\n", cmd, i-NUM_READERS); shutdown_clients(); exit(0); } } } } ```