code wiki / (root) / nx_remote_worker_test.nx

nx_remote_worker_test.nx source

↩ module page · 57 lines · 1963 B

1// nx_remote_worker_test.nx -- prove distributed-MIMD dispatch via 2// nx_remote_worker. Orchestrator dispatches 50 tasks of the form 3// `f(id, arg) = arg*arg + id` to a worker thread; verifies all 4// results. 5 6import "nx_kernel_v2.nx" 7import "nx_log.nx" 8import "nx_atom.nx" 9import "nx_net_chan.nx" 10import "nx_remote_worker.nx" 11 12const NX_PORT: i64 = 38921 13const N_TASKS: i64 = 50 14 15func task_handler(id: i64, arg: i64) -> i64 { 16 return arg * arg + id 17} 18 19func main() -> nx_exit { 20 println("=== nx_remote_worker distributed dispatch smoke ===" as *u8) 21 println("port:" as *u8); print_i64(NX_PORT); println("" as *u8) 22 println("tasks:" as *u8); print_i64(N_TASKS); println("" as *u8) 23 24 let w: *NxRemoteWorker = nx_remote_worker_start(task_handler, NX_PORT) 25 if nx_atom_load_i64(((w as i64) + 16) as *i64, NX_MO_SEQ_CST) != 1 { 26 println("FAIL: worker never reached ready" as *u8); return 1 27 } 28 println("worker listening + ready" as *u8) 29 30 let nc: *NxNetChan = nx_remote_orchestrator_connect(NX_PORT) 31 if (nc as i64) == 0 { println("FAIL: orchestrator connect" as *u8); return 2 } 32 33 let out_raw: *u8 = sys_mmap(16) 34 let out: *i64 = out_raw as *i64 35 36 var i: i64 = 0 37 while i < N_TASKS { 38 let arg: i64 = i + 3 39 if nx_remote_dispatch(nc, i, arg, out) != 0 { 40 println("FAIL: dispatch" as *u8); return 3 41 } 42 let expected: i64 = arg * arg + i 43 if *out != expected { 44 println("FAIL: result mismatch" as *u8) 45 print_i64(*out); println("" as *u8) 46 print_i64(expected); println("" as *u8) 47 return 4 48 } 49 i = i + 1 50 } 51 nx_remote_shutdown(nc) 52 53 println("PASS: 50 tasks dispatched + computed remotely + results verified." as *u8) 54 println("Distributed-MIMD compute pattern proven (over loopback today;" as *u8) 55 println("same wire protocol works cross-host when worker runs on remote node)." as *u8) 56 return 0 57}