int main(int argc, char *argv[]) { initTracer(); int i; int rank, size; scene *curscene; int res; TGAFILE* tga = NULL; vector *scanline; MPI_Status status; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); if (rank == 0) { curscene = sceneLoad(); tga = openTGA(curscene); } else curscene = calloc(1, sizeof(scene)); MPI_Bcast((void*)curscene, sizeof(scene) / sizeof(float), MPI_FLOAT, 0, MPI_COMM_WORLD); res = sceneResolution(curscene); scanline = calloc(res, sizeof(vector)); int line = 0; float aaweight = 1.0f / size; for (line = 0; line < res; line++) { if (rank == size - 1) { printf("Starting scan of horizontal line %d.\n", line); } else { MPI_Recv(scanline, (sizeof(vector) / sizeof(float)) * res, MPI_FLOAT, rank + 1, MPI_ANY_TAG, MPI_COMM_WORLD, &status); } for (i = 0; i <res; i++) { if (rank == size - 1) scanline[i] = smul(pixelraytraceaa(curscene, line, i, rank), aaweight); else scanline[i] = add(scanline[i], smul(pixelraytraceaa(curscene, line, i, rank), aaweight)); } if (rank == 0) { for (i = 0; i < res; i++) writeTGAColor(tga, curscene, line, i, scanline[i].x, scanline[i].y, scanline[i].z); } else { MPI_Send(scanline, (sizeof(vector) / sizeof(float)) * res, MPI_FLOAT, rank - 1, 0, MPI_COMM_WORLD); } } free(curscene); MPI_Finalize(); return 0; }
void *Calloc(size_t nmemb, size_t size) { void *ret; /* older libcs don't check for int overflow */ smul(nmemb, size); ret = calloc(nmemb, size); if (ret == NULL) die("calloc"); return ret; }
static_assert(mul<int>(17,22) == Result<int>{false, 374}); static_assert(mul<int>(INT_MAX / 22, 23) == Result<int>{true, -2049870757}); static_assert(mul<int>(INT_MIN / 22, -23) == Result<int>{true, -2049870757}); constexpr Result<int> sadd(int lhs, int rhs) { int sum{}; return {__builtin_sadd_overflow(lhs, rhs, &sum), sum}; } static_assert(sadd(17,22) == Result<int>{false, 39}); static_assert(sadd(INT_MAX - 22, 23) == Result<int>{true, INT_MIN}); static_assert(sadd(INT_MIN + 22, -23) == Result<int>{true, INT_MAX}); constexpr Result<int> ssub(int lhs, int rhs) { int sum{}; return {__builtin_ssub_overflow(lhs, rhs, &sum), sum}; } static_assert(ssub(17,22) == Result<int>{false, -5}); static_assert(ssub(INT_MAX - 22, -23) == Result<int>{true, INT_MIN}); static_assert(ssub(INT_MIN + 22, 23) == Result<int>{true, INT_MAX}); constexpr Result<int> smul(int lhs, int rhs) { int sum{}; return {__builtin_smul_overflow(lhs, rhs, &sum), sum}; } static_assert(smul(17,22) == Result<int>{false, 374}); static_assert(smul(INT_MAX / 22, 23) == Result<int>{true, -2049870757}); static_assert(smul(INT_MIN / 22, -23) == Result<int>{true, -2049870757});
int main() { freopen("saferoute.in", "r", stdin); freopen("saferoute.out", "w", stdout); int n; scanf("%d", &n); for (int i = 0; i < n; i++) { int x1, y1, x2, y2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); a[i] = y2 - y1; b[i] = x1 - x2; c[i] = -a[i] * x1 - b[i] * y1; } for (int i = 0; i < 2; i++) { int x, y; scanf("%d%d", &x, &y); px[i] = x; py[i] = y; } int cn = 2; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int z = a[i] * b[j] - b[i] * a[j]; if (z == 0) continue; double cx = 1. * (b[i] * c[j] - b[j] * c[i]) / z; double cy = 1. * (c[i] * a[j] - a[i] * c[j]) / z; bool found = false; for (int k = 0; k < cn; k++) { if (comp(cx, px[k]) == 0 && comp(cy, py[k]) == 0) { found = true; break; } } if (!found) { px[cn] = cx; py[cn++] = cy; } } } for (int i = 0; i < cn; i++) id[i] = i; for (int i = 0; i < cn; i++) for (int j = i + 1; j < cn; j++) { int c1 = comp(px[i], px[j]); int c2 = comp(py[i], py[j]); if (c1 > 0 || (c1 == 0 && c2 > 0)) { std::swap(px[i], px[j]); std::swap(py[i], py[j]); std::swap(id[i], id[j]); } } int start = -1; int finish = -1; for (int i = 0; i < cn; i++) if (id[i] == 0) start = i; else if (id[i] == 1) finish = i; int cs = 0; for (int i = 0; i < n; i++) { int cc = 0; for (int j = 0; j < cn; j++) { double f = a[i] * px[j] + b[i] * py[j] + c[i]; if (comp(f, 0) == 0) { q[cc++] = j; } } for (int j = 0; j + 1 < cc; j++) { st[cs] = q[j]; fi[cs] = q[j + 1]; cs++; st[cs] = q[j + 1]; fi[cs] = q[j]; cs++; } } for (int i = 0; i < cs; i++) { d[i] = INF; } for (int i = 0; i < cs; i++) { if (st[i] == start) d[i] = 0; } for (int i = 0; i < cs; i++) vx[i] = px[fi[i]] - px[st[i]], vy[i] = py[fi[i]] - py[st[i]]; while (true) { int mi = -1; for (int i = 0; i < cs; i++) { if (was[i] || d[i] == INF) continue; if (mi < 0 || d[mi] > d[i]) mi = i; } if (mi < 0) break; was[mi] = true; for (int i = 0; i < cs; i++) { if (st[i] != fi[mi]) continue; double ang = atan2(vmul(mi, i), smul(mi, i)); if (ang < 0) ang = -ang; if (d[i] > d[mi] + ang) d[i] = d[mi] + ang; } } double ans = INF; for (int i = 0; i < cs; i++) { if (fi[i] == finish) { if (ans > d[i]) ans = d[i]; } } if (ans == INF) puts("-1"); else { printf("%.17lf\n", ans / PI * 180); } }
CompileOutput *Compiler::Compile(AMXRef amx) { Prepare(amx); Disassembler disasm(amx); Instruction instr; bool error = false; while (!error && disasm.Decode(instr, error)) { if (!Process(instr)) { error = true; break; } switch (instr.opcode().GetId()) { case OP_LOAD_PRI: load_pri(instr.operand()); break; case OP_LOAD_ALT: load_alt(instr.operand()); break; case OP_LOAD_S_PRI: load_s_pri(instr.operand()); break; case OP_LOAD_S_ALT: load_s_alt(instr.operand()); break; case OP_LREF_PRI: lref_pri(instr.operand()); break; case OP_LREF_ALT: lref_alt(instr.operand()); break; case OP_LREF_S_PRI: lref_s_pri(instr.operand()); break; case OP_LREF_S_ALT: lref_s_alt(instr.operand()); break; case OP_LOAD_I: load_i(); break; case OP_LODB_I: lodb_i(instr.operand()); break; case OP_CONST_PRI: const_pri(instr.operand()); break; case OP_CONST_ALT: const_alt(instr.operand()); break; case OP_ADDR_PRI: addr_pri(instr.operand()); break; case OP_ADDR_ALT: addr_alt(instr.operand()); break; case OP_STOR_PRI: stor_pri(instr.operand()); break; case OP_STOR_ALT: stor_alt(instr.operand()); break; case OP_STOR_S_PRI: stor_s_pri(instr.operand()); break; case OP_STOR_S_ALT: stor_s_alt(instr.operand()); break; case OP_SREF_PRI: sref_pri(instr.operand()); break; case OP_SREF_ALT: sref_alt(instr.operand()); break; case OP_SREF_S_PRI: sref_s_pri(instr.operand()); break; case OP_SREF_S_ALT: sref_s_alt(instr.operand()); break; case OP_STOR_I: stor_i(); break; case OP_STRB_I: strb_i(instr.operand()); break; case OP_LIDX: lidx(); break; case OP_LIDX_B: lidx_b(instr.operand()); break; case OP_IDXADDR: idxaddr(); break; case OP_IDXADDR_B: idxaddr_b(instr.operand()); break; case OP_ALIGN_PRI: align_pri(instr.operand()); break; case OP_ALIGN_ALT: align_alt(instr.operand()); break; case OP_LCTRL: lctrl(instr.operand(), instr.address() + instr.size()); break; case OP_SCTRL: sctrl(instr.operand()); break; case OP_MOVE_PRI: move_pri(); break; case OP_MOVE_ALT: move_alt(); break; case OP_XCHG: xchg(); break; case OP_PUSH_PRI: push_pri(); break; case OP_PUSH_ALT: push_alt(); break; case OP_PUSH_C: push_c(instr.operand()); break; case OP_PUSH: push(instr.operand()); break; case OP_PUSH_S: push_s(instr.operand()); break; case OP_POP_PRI: pop_pri(); break; case OP_POP_ALT: pop_alt(); break; case OP_STACK: // value stack(instr.operand()); break; case OP_HEAP: heap(instr.operand()); break; case OP_PROC: proc(); break; case OP_RET: ret(); break; case OP_RETN: retn(); break; case OP_JUMP_PRI: jump_pri(); break; case OP_CALL: case OP_JUMP: case OP_JZER: case OP_JNZ: case OP_JEQ: case OP_JNEQ: case OP_JLESS: case OP_JLEQ: case OP_JGRTR: case OP_JGEQ: case OP_JSLESS: case OP_JSLEQ: case OP_JSGRTR: case OP_JSGEQ: { cell dest = instr.operand() - reinterpret_cast<cell>(amx.code()); switch (instr.opcode().GetId()) { case OP_CALL: call(dest); break; case OP_JUMP: jump(dest); break; case OP_JZER: jzer(dest); break; case OP_JNZ: jnz(dest); break; case OP_JEQ: jeq(dest); break; case OP_JNEQ: jneq(dest); break; case OP_JLESS: jless(dest); break; case OP_JLEQ: jleq(dest); break; case OP_JGRTR: jgrtr(dest); break; case OP_JGEQ: jgeq(dest); break; case OP_JSLESS: jsless(dest); break; case OP_JSLEQ: jsleq(dest); break; case OP_JSGRTR: jsgrtr(dest); break; case OP_JSGEQ: jsgeq(dest); break; } break; } case OP_SHL: shl(); break; case OP_SHR: shr(); break; case OP_SSHR: sshr(); break; case OP_SHL_C_PRI: shl_c_pri(instr.operand()); break; case OP_SHL_C_ALT: shl_c_alt(instr.operand()); break; case OP_SHR_C_PRI: shr_c_pri(instr.operand()); break; case OP_SHR_C_ALT: shr_c_alt(instr.operand()); break; case OP_SMUL: smul(); break; case OP_SDIV: sdiv(); break; case OP_SDIV_ALT: sdiv_alt(); break; case OP_UMUL: umul(); break; case OP_UDIV: udiv(); break; case OP_UDIV_ALT: udiv_alt(); break; case OP_ADD: add(); break; case OP_SUB: sub(); break; case OP_SUB_ALT: sub_alt(); break; case OP_AND: and_(); break; case OP_OR: or_(); break; case OP_XOR: xor_(); break; case OP_NOT: not_(); break; case OP_NEG: neg(); break; case OP_INVERT: invert(); break; case OP_ADD_C: add_c(instr.operand()); break; case OP_SMUL_C: smul_c(instr.operand()); break; case OP_ZERO_PRI: zero_pri(); break; case OP_ZERO_ALT: zero_alt(); break; case OP_ZERO: zero(instr.operand()); break; case OP_ZERO_S: zero_s(instr.operand()); break; case OP_SIGN_PRI: sign_pri(); break; case OP_SIGN_ALT: sign_alt(); break; case OP_EQ: eq(); break; case OP_NEQ: neq(); break; case OP_LESS: less(); break; case OP_LEQ: leq(); break; case OP_GRTR: grtr(); break; case OP_GEQ: geq(); break; case OP_SLESS: sless(); break; case OP_SLEQ: sleq(); break; case OP_SGRTR: sgrtr(); break; case OP_SGEQ: sgeq(); break; case OP_EQ_C_PRI: eq_c_pri(instr.operand()); break; case OP_EQ_C_ALT: eq_c_alt(instr.operand()); break; case OP_INC_PRI: inc_pri(); break; case OP_INC_ALT: inc_alt(); break; case OP_INC: inc(instr.operand()); break; case OP_INC_S: inc_s(instr.operand()); break; case OP_INC_I: inc_i(); break; case OP_DEC_PRI: dec_pri(); break; case OP_DEC_ALT: dec_alt(); break; case OP_DEC: dec(instr.operand()); break; case OP_DEC_S: dec_s(instr.operand()); break; case OP_DEC_I: dec_i(); break; case OP_MOVS: movs(instr.operand()); break; case OP_CMPS: cmps(instr.operand()); break; case OP_FILL: fill(instr.operand()); break; case OP_HALT: halt(instr.operand()); break; case OP_BOUNDS: bounds(instr.operand()); break; case OP_SYSREQ_PRI: sysreq_pri(); break; case OP_SYSREQ_C: { const char *name = amx.GetNativeName(instr.operand()); if (name == 0) { error = true; } else { sysreq_c(instr.operand(), name); } break; } case OP_SYSREQ_D: { const char *name = amx.GetNativeName(amx.FindNative(instr.operand())); if (name == 0) { error = true; } else { sysreq_d(instr.operand(), name); } break; } case OP_SWITCH: switch_(CaseTable(amx, instr.operand())); break; case OP_CASETBL: casetbl(); break; case OP_SWAP_PRI: swap_pri(); break; case OP_SWAP_ALT: swap_alt(); break; case OP_PUSH_ADR: push_adr(instr.operand()); break; case OP_NOP: nop(); break; case OP_BREAK: break_(); break; default: error = true; } } if (error && error_handler_ != 0) { error_handler_->Execute(instr); } return Finish(error); }
/********************************************************************************* * * * G A T E W A Y R O U T I N E * * * *********************************************************************************/ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { /* >>>>>>>>>>>>>>>>>> VARIABLE DECLARATIONS <<<<<<<<<<<<<<<<<<< */ matrix *PI_vector; matrix *NetDef, *W1, *W2, *PHI, *Y, *L_hidden, *H_hidden; double *M, lambda; int iter, hidden, inputs, outputs, a, n, decays; trparmstruct *trparms; mxArray *Matmatrix; char *infolevelstr[] = {"infolevel", "Infolevel", "INFOLEVEL", "InfoLevel"}; char *maxiterstr[] = {"maxiter", "MAXITER", "Maxiter", "MaxIter"}; char *critminstr[] = {"critmin", "Critmin", "CRITMIN", "CritMin"}; char *crittermstr[] = {"critterm", "Critterm", "CRITTERM", "CritTerm"}; char *gradtermstr[] = {"gradterm", "Gradterm", "GRADTERM", "GradTerm"}; char *paramtermstr[] = {"paramterm", "Paramterm", "PARAMTERM", "ParamTerm"}; char *Dstr[] = {"D", "d"}; char *lambdastr[] = {"lambda", "Lambda", "LAMBDA"}; char *skipstr[] = {"skip", "Skip", "SKIP"}; /* >>>>>>>>>>>>>>>> CHECK FOR PROPER NUMBER OF ARGUMENTS <<<<<<<<<<<<<<< */ if (nrhs<5 || nrhs>6) { mexErrMsgTxt("Wrong number of input arguments"); } else if (nlhs > 5) { mexErrMsgTxt("Too many output arguments"); } /* >>>>>>>>>>>>>>>>> CONVERT INPUT ARGUMENTS TO SM FORMAT <<<<<<<<<<<<<<<< */ NetDef = matstring2sm(prhs[0]); PHI = mat2sm(prhs[3]); Y = mat2sm(prhs[4]); inputs = mxGetM(prhs[3]); outputs = mxGetM(prhs[4]); L_hidden = neuvector(NetDef,1,'L'); /* Location of linear hidden units */ H_hidden = neuvector(NetDef,1,'H'); /* Location of tanh hidden units */ hidden = L_hidden->row*L_hidden->col + H_hidden->row*H_hidden->col; if(mxGetM(prhs[1])==0 || mxGetN(prhs[1])==0 || mxGetM(prhs[2])==0\ || mxGetN(prhs[2])==0){ W1 = mmake(hidden,inputs+1); W2 = mmake(outputs,hidden+1); mrand(W1); smul(W1,W1,0.5); mrand(W2); smul(W2,W2,0.5); } else{ if(mxGetM(prhs[1])!=hidden) mexErrMsgTxt("W1 has the wrong dimension"); if(mxGetN(prhs[1])!=inputs+1) mexErrMsgTxt("W1 has the wrong dimension"); if(mxGetM(prhs[2])!=outputs) mexErrMsgTxt("W2 has the wrong dimension"); if(mxGetN(prhs[2])!=hidden+1) mexErrMsgTxt("W2 has the wrong dimension"); W1 = mat2sm(prhs[1]); /* Input-to-hidden layer weights */ W2 = mat2sm(prhs[2]); /* Hidden-to-output layer weights */ } trparms = (trparmstruct*)malloc(sizeof(trparmstruct)); a = 5; if (nrhs==6){ /* INFOLEVEL */ trparms->infolevel = TRDINFOLEVEL; for(n=0;n<4;n++){ if ((Matmatrix=mxGetField(prhs[a], 0, infolevelstr[n]))!=NULL){ trparms->infolevel=(int)(*mxGetPr(Matmatrix)); break; } } /* MAXITER */ trparms->maxiter = TRDMAXITER; for(n=0;n<4;n++){ if ((Matmatrix=mxGetField(prhs[a], 0, maxiterstr[n]))!=NULL){ trparms->maxiter=(int)(*mxGetPr(Matmatrix)); break; } } /* CRITMIN */ trparms->critmin = TRDCRITMIN; for(n=0;n<4;n++){ if ((Matmatrix=mxGetField(prhs[a], 0, critminstr[n]))!=NULL){ trparms->critmin=(double)(*mxGetPr(Matmatrix)); break; } } /* CRITTERM */ trparms->critterm = TRDCRITTERM; for(n=0;n<4;n++){ if ((Matmatrix=mxGetField(prhs[a], 0, crittermstr[n]))!=NULL){ trparms->critterm=(double)(*mxGetPr(Matmatrix)); break; } } /* GRADTERM */ trparms->gradterm = TRDGRADTERM; for(n=0;n<4;n++){ if ((Matmatrix=mxGetField(prhs[a], 0, gradtermstr[n]))!=NULL){ trparms->gradterm=(double)(*mxGetPr(Matmatrix)); break; } } /* PARAMTERM */ trparms->paramterm = TRDPARAMTERM; for(n=0;n<4;n++){ if ((Matmatrix=mxGetField(prhs[a], 0, paramtermstr[n]))!=NULL){ trparms->paramterm=(double)(*mxGetPr(Matmatrix)); break; } } /* Lambda */ trparms->lambda = TRDLAMBDA; for(n=0;n<3;n++){ if ((Matmatrix=mxGetField(prhs[a], 0, lambdastr[n]))!=NULL){ trparms->lambda=(double)(*mxGetPr(Matmatrix)); break; } } /* D */ for(n=0;n<2;n++){ if ((Matmatrix=mxGetField(prhs[a], 0, Dstr[n]))!=NULL){ decays = mxGetM(Matmatrix)*mxGetN(Matmatrix); trparms->D = mmake(1,decays); M = mxGetPr(Matmatrix); for(n=0;n<decays;n++){ rvput(trparms->D,n,M[n]); } break; } } if(Matmatrix==NULL){ trparms->D = mmake(1,1); put_val(trparms->D,0,0,TRDD); } } else { trparms->infolevel = TRDINFOLEVEL; trparms->maxiter = TRDMAXITER; trparms->critmin = TRDCRITMIN; trparms->critterm = TRDCRITTERM; trparms->gradterm = TRDGRADTERM; trparms->paramterm = TRDPARAMTERM; trparms->D = mmake(1,1); put_val(trparms->D,0,0,TRDD); trparms->lambda = TRDLAMBDA; trparms->skip = TRDSKIP; } /* >>>>>>>>>>>>>>>>>>>>>> CALL THE C-ROUTINE <<<<<<<<<<<<<<<<<<<<< */ marqc(&PI_vector, &iter, &lambda, NetDef, W1, W2, PHI, Y, trparms); /* >>>>>>>>>>>>>>>>>>> CREATE OUTPUT MATICES <<<<<<<<<<<<<<<<<< */ plhs[0] = mxCreateDoubleMatrix(getrows(W1),getcols(W1),mxREAL); plhs[1] = mxCreateDoubleMatrix(getrows(W2),getcols(W2),mxREAL); plhs[2] = mxCreateDoubleMatrix(getrows(PI_vector),getcols(PI_vector),mxREAL); plhs[3] = mxCreateDoubleMatrix(1,1,mxREAL); plhs[4] = mxCreateDoubleMatrix(1,1,mxREAL); sm2mat(plhs[0],W1); sm2mat(plhs[1],W2); sm2mat(plhs[2],PI_vector); M = mxGetPr(plhs[3]); M[0] = (double)iter; M = mxGetPr(plhs[4]); M[0] = (double)lambda; /* >>>>>>>>>>>>>>>>>>>> FREE ARGUMENT MATRICES <<<<<<<<<<<<<<<<<<<<< */ mfree(NetDef); mfree(PHI); mfree(Y); mfree(L_hidden); mfree(H_hidden); mfree(trparms->D); free(trparms); }