daemon.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /**
  2. * \file daemon.c
  3. *
  4. * \brief Implementation of the server for parallel
  5. * computing of r.li raster analysis
  6. *
  7. * \author Claudio Porta & Lucio Davide Spano
  8. *
  9. * This program is free software under the GPL (>=v2)
  10. * Read the COPYING file that comes with GRASS for details.
  11. *
  12. * \version 1.0
  13. *
  14. * \include
  15. *
  16. */
  17. #include <grass/config.h>
  18. #include <stdlib.h>
  19. #include <stddef.h>
  20. #include <fcntl.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <dirent.h>
  24. #include <errno.h>
  25. #include <math.h>
  26. #ifdef __MINGW32__
  27. #include <process.h>
  28. #else
  29. #include <sys/wait.h>
  30. #endif
  31. #include <grass/gis.h>
  32. #include <grass/raster.h>
  33. #include <grass/glocale.h>
  34. #include "daemon.h"
  35. #ifdef __MINGW32__
  36. #define srandom srand
  37. #define random rand
  38. #endif
  39. int calculateIndex(char *file, int f(int, char **, area_des, double *),
  40. char **parameters, char *raster, char *output)
  41. {
  42. char pathSetup[GPATH_MAX], out[GPATH_MAX], parsed;
  43. char *random_access_name;
  44. struct History history;
  45. g_areas g;
  46. int res;
  47. int i, doneDir, mv_fd, random_access;
  48. /*int mv_rows, mv_cols; */
  49. list l;
  50. msg m, doneJob;
  51. /* int perc=0; */
  52. g = (g_areas) G_malloc(sizeof(struct generatore));
  53. l = (list) G_malloc(sizeof(struct lista));
  54. worker_init(raster, f, parameters);
  55. /*########################################################
  56. -----------------create area queue----------------------
  57. ######################################################### */
  58. /* strip off leading path if present */
  59. char testpath[GPATH_MAX];
  60. sprintf(testpath, "%s%s", G_home(), "/.grass7/r.li/");
  61. if(strncmp(file, testpath, strlen(testpath)) == 0)
  62. file += strlen(testpath);
  63. /* TODO: check if this path is portable */
  64. /* TODO: use G_rc_path() */
  65. sprintf(pathSetup, "%s/.grass7/r.li/%s", G_home(), file);
  66. G_debug(1, "r.li.daemon pathSetup: [%s]", pathSetup);
  67. parsed = parseSetup(pathSetup, l, g, raster);
  68. /*########################################################
  69. -----------------open output file ---------------------
  70. ####################################################### */
  71. if (parsed == MVWIN) {
  72. /* struct Cell_head cellhd_r, cellhd_new;
  73. char *mapset; */
  74. /*creating new raster file */
  75. mv_fd = Rast_open_new(output, DCELL_TYPE);
  76. random_access_name = G_tempfile();
  77. random_access = open(random_access_name, O_RDWR | O_CREAT, 0755);
  78. if (random_access == -1)
  79. G_fatal_error(_("Cannot create random access file"));
  80. }
  81. else {
  82. /* check if ~/.grass7/ exists */
  83. sprintf(out, "%s/.grass7/", G_home());
  84. doneDir = G_mkdir(out);
  85. if (doneDir == -1 && errno != EEXIST)
  86. G_fatal_error(_("Cannot create %s/.grass7/ directory"),
  87. G_home());
  88. /* check if ~/.grass7/r.li/ exists */
  89. sprintf(out, "%s/.grass7/r.li/", G_home());
  90. doneDir = G_mkdir(out);
  91. if (doneDir == -1 && errno != EEXIST)
  92. G_fatal_error(_("Cannot create %s/.grass7/r.li/ directory"),
  93. G_home());
  94. /* check if ~/.grass7/r.li/output exists */
  95. sprintf(out, "%s/.grass7/r.li/output", G_home());
  96. doneDir = G_mkdir(out);
  97. if (doneDir == -1 && errno != EEXIST)
  98. G_fatal_error(_("Cannot create %s/.grass7/r.li/output/ directory"),
  99. G_home());
  100. sprintf(out, "%s/.grass7/r.li/output/%s", G_home(), output);
  101. res = open(out, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  102. }
  103. i = 0;
  104. /*#######################################################
  105. ------------------analysis loop----------------------
  106. ####################################################### */
  107. /*first job scheduling */
  108. if (next_Area(parsed, l, g, &m) != 0)
  109. /*body */
  110. while (next_Area(parsed, l, g, &m) != 0) {
  111. worker_process(&doneJob, &m);
  112. /*perc++; */
  113. /*G_percent (perc, WORKERS, 1); */
  114. if (doneJob.type == DONE) {
  115. double result;
  116. result = doneJob.f.f_d.res;
  117. /*output */
  118. if (parsed != MVWIN) {
  119. print_Output(res, doneJob);
  120. }
  121. else {
  122. /*raster output */
  123. raster_Output(random_access, doneJob.f.f_d.aid, g,
  124. doneJob.f.f_d.res);
  125. }
  126. }
  127. else {
  128. if (parsed != MVWIN) {
  129. error_Output(res, doneJob);
  130. }
  131. else {
  132. /*printf("todo ");fflush(stdout); *//* TODO scrivere su raster NULL ??? */
  133. }
  134. }
  135. }
  136. worker_end();
  137. /*################################################
  138. --------------delete tmp files------------------
  139. ################################################ */
  140. if (parsed == MVWIN) {
  141. write_raster(mv_fd, random_access, g);
  142. close(random_access);
  143. unlink(random_access_name);
  144. Rast_close(mv_fd);
  145. Rast_short_history(output, "raster", &history);
  146. Rast_command_history(&history);
  147. Rast_write_history(output, &history);
  148. }
  149. return 1;
  150. }
  151. int parseSetup(char *path, list l, g_areas g, char *raster)
  152. {
  153. STRUCT_STAT s;
  154. struct Cell_head cellhd;
  155. char *buf;
  156. const char *token;
  157. int setup;
  158. int letti;
  159. double rel_x, rel_y, rel_rl, rel_cl;
  160. double sf_n, sf_s, sf_e, sf_w;
  161. int sf_x, sf_y, sf_rl, sf_cl;
  162. int size;
  163. if (stat(path, &s) != 0)
  164. G_fatal_error(_("Cannot find configuration file <%s>"), path);
  165. size = s.st_size * sizeof(char);
  166. buf = G_malloc(size);
  167. setup = open(path, O_RDONLY, 0755);
  168. if (setup == -1)
  169. G_fatal_error(_("Cannot read setup file"));
  170. letti = read(setup, buf, s.st_size);
  171. if (letti < s.st_size)
  172. G_fatal_error(_("Cannot read setup file"));
  173. token = strtok(buf, " ");
  174. if (strcmp("SAMPLINGFRAME", token) != 0)
  175. G_fatal_error(_("Unable to parse configuration file"));
  176. rel_x = atof(strtok(NULL, "|"));
  177. rel_y = atof(strtok(NULL, "|"));
  178. rel_rl = atof(strtok(NULL, "|"));
  179. rel_cl = atof(strtok(NULL, "\n"));
  180. /* find raster map */
  181. Rast_get_cellhd(raster, "", &cellhd);
  182. /* calculate absolute sampling frame definition */
  183. sf_x = (int)rint(cellhd.cols * rel_x);
  184. sf_y = (int)rint(cellhd.rows * rel_y);
  185. sf_rl = (int)rint(cellhd.rows * rel_rl);
  186. sf_cl = (int)rint(cellhd.cols * rel_cl);
  187. /* calculate sample frame boundaries */
  188. sf_n = cellhd.north - (cellhd.ns_res * sf_y);
  189. sf_s = sf_n - (cellhd.ns_res * sf_rl);
  190. sf_w = cellhd.west + (cellhd.ew_res * sf_x);
  191. sf_e = sf_w + (cellhd.ew_res * sf_cl);
  192. /* parse configuration file */
  193. token = strtok(NULL, " ");
  194. if (strcmp("SAMPLEAREA", token) == 0) {
  195. double rel_sa_x, rel_sa_y, rel_sa_rl, rel_sa_cl;
  196. int aid = 1, toReturn;
  197. do {
  198. rel_sa_x = atof(strtok(NULL, "|"));
  199. rel_sa_y = atof(strtok(NULL, "|"));
  200. rel_sa_rl = atof(strtok(NULL, "|"));
  201. rel_sa_cl = atof(strtok(NULL, "\n"));
  202. if (rel_sa_x == -1.0 && rel_sa_y == -1.0) {
  203. /* runtime disposition */
  204. int sa_rl, sa_cl;
  205. sa_rl = (int)rint(cellhd.rows * rel_sa_rl);
  206. sa_cl = (int)rint(cellhd.cols * rel_sa_cl);
  207. g->rows = sf_rl;
  208. g->cols = sf_cl;
  209. g->rl = sa_rl;
  210. g->cl = sa_cl;
  211. g->count = 1;
  212. g->sf_x = sf_x;
  213. g->sf_y = sf_y;
  214. g->x = sf_x;
  215. g->y = sf_y;
  216. g->maskname = NULL;
  217. return disposeAreas(l, g, strtok(NULL, "\n"));
  218. }
  219. else {
  220. msg m;
  221. toReturn = NORMAL;
  222. /*read file and create list */
  223. m.type = AREA;
  224. m.f.f_a.x = (int)rint(cellhd.cols * rel_sa_x);
  225. m.f.f_a.y = (int)rint(cellhd.rows * rel_sa_y);
  226. m.f.f_a.rl = (int)rint(cellhd.rows * rel_sa_rl);
  227. m.f.f_a.cl = (int)rint(cellhd.cols * rel_sa_cl);
  228. m.f.f_a.aid = aid;
  229. aid++;
  230. insertNode(l, m);
  231. }
  232. } while ((token = strtok(NULL, " ")) != NULL &&
  233. strcmp(token, "SAMPLEAREA") == 0);
  234. close(setup);
  235. return toReturn;
  236. }
  237. else if (strcmp("MASKEDSAMPLEAREA", token) == 0) {
  238. double rel_sa_x, rel_sa_y, rel_sa_rl, rel_sa_cl;
  239. int aid = 1;
  240. char maskname[GNAME_MAX];
  241. do {
  242. rel_sa_x = atof(strtok(NULL, "|"));
  243. rel_sa_y = atof(strtok(NULL, "|"));
  244. rel_sa_rl = atof(strtok(NULL, "|"));
  245. rel_sa_cl = atof(strtok(NULL, "|"));
  246. strcpy(maskname, strtok(NULL, "\n"));
  247. if (rel_sa_x == -1 && rel_sa_y == -1) {
  248. /* runtime disposition */
  249. int sa_rl, sa_cl;
  250. sa_rl = (int)rint(cellhd.rows * rel_sa_rl);
  251. sa_cl = (int)rint(cellhd.cols * rel_sa_cl);
  252. g->rows = sf_rl;
  253. g->cols = sf_cl;
  254. g->rl = sa_rl;
  255. g->cl = sa_cl;
  256. g->count = 1;
  257. g->x = sf_x;
  258. g->y = sf_y;
  259. g->maskname = maskname;
  260. return disposeAreas(l, g, strtok(NULL, "\n"));
  261. }
  262. else {
  263. /*read file and create list */
  264. msg m;
  265. m.type = MASKEDAREA;
  266. m.f.f_ma.x = (int)rint(cellhd.cols * rel_sa_x);
  267. m.f.f_ma.y = (int)rint(cellhd.rows * rel_sa_y);
  268. m.f.f_ma.rl = (int)rint(cellhd.rows * rel_sa_rl);
  269. m.f.f_ma.cl = (int)rint(cellhd.cols * rel_sa_cl);
  270. m.f.f_ma.aid = aid;
  271. strcpy(m.f.f_ma.mask, maskname);
  272. aid++;
  273. insertNode(l, m);
  274. }
  275. }
  276. while ((token = strtok(NULL, " ")) != NULL &&
  277. strcmp(token, "MASKEDSAMPLEAREA") == 0);
  278. close(setup);
  279. return NORMAL;
  280. }
  281. else if (strcmp("MASKEDOVERLAYAREA", token) == 0) {
  282. double sa_n, sa_s, sa_w, sa_e;
  283. int aid = 1;
  284. char maskname[GNAME_MAX];
  285. msg m;
  286. do {
  287. strcpy(maskname, strtok(NULL, "|"));
  288. sa_n = atof(strtok(NULL, "|"));
  289. sa_s = atof(strtok(NULL, "|"));
  290. sa_e = atof(strtok(NULL, "|"));
  291. sa_w = atof(strtok(NULL, "\n"));
  292. m.type = MASKEDAREA;
  293. m.f.f_ma.x = (int)rint((cellhd.north - sa_n) * cellhd.ns_res);
  294. m.f.f_ma.y = (int)rint((cellhd.west + sa_w) * cellhd.ew_res);
  295. m.f.f_ma.rl = (int)rint((sa_n - sa_s) * cellhd.ns_res);
  296. m.f.f_ma.cl = (int)rint((sa_e - sa_w) * cellhd.ew_res);
  297. m.f.f_ma.aid = aid;
  298. strcpy(m.f.f_ma.mask, maskname);
  299. aid++;
  300. insertNode(l, m);
  301. }
  302. while ((token = strtok(NULL, " ")) != NULL &&
  303. (strcmp(token, "MASKEDOVERLAYAREA") == 0));
  304. if (strcmp(token, "RASTERMAP") != 0)
  305. G_fatal_error(_("Irregular maskedoverlay areas definition"));
  306. token = strtok(NULL, "\n");
  307. if (strcmp(token, raster) != 0)
  308. G_fatal_error(_("The configuration file can be used only with \
  309. %s rasterfile"), token);
  310. close(setup);
  311. return NORMAL;
  312. }
  313. else
  314. G_fatal_error(_("Illegal configuration file (sample area)"));
  315. close(setup);
  316. return ERROR;
  317. }
  318. int disposeAreas(list l, g_areas g, char *def)
  319. {
  320. char *token;
  321. token = strtok(def, " \n");
  322. if (strcmp(token, "MOVINGWINDOW") == 0) {
  323. g->count = 0;
  324. g->dist = 0;
  325. g->add_row = 1;
  326. g->add_col = 1;
  327. if (g->rl != 1)
  328. g->rows = g->rows - g->rl + 1;
  329. else
  330. g->rows = g->rows;
  331. if (g->cl != 1)
  332. g->cols = g->cols - g->cl + 1;
  333. return MVWIN;
  334. }
  335. else if (strcmp(token, "RANDOMNONOVERLAPPING") == 0) {
  336. int units, sf_rl, sf_cl, sa_rl, sa_cl, max_units, i;
  337. int *assigned;
  338. sscanf(strtok(NULL, "\n"), "%i", &units);
  339. sf_rl = g->rows;
  340. sf_cl = g->cols;
  341. sa_rl = g->rl;
  342. sa_cl = g->cl;
  343. max_units = (int)rint((sf_rl / sa_rl) * (sf_cl / sa_cl));
  344. if (units > max_units)
  345. G_fatal_error(_("Too many units to place"));
  346. assigned = G_malloc(units * sizeof(int));
  347. i = 0;
  348. srandom(0);
  349. while (i < units) {
  350. int j, position, found = FALSE;
  351. position = random() % max_units;
  352. for (j = 0; j < i; j++) {
  353. if (assigned[j] == position)
  354. found = TRUE;
  355. }
  356. if (!found) {
  357. msg m;
  358. assigned[i] = position;
  359. i++;
  360. if (g->maskname == NULL) {
  361. int n_col = rint(sf_cl / sa_cl);
  362. m.type = AREA;
  363. m.f.f_a.aid = i;
  364. m.f.f_a.x = g->sf_x + (position % n_col) * sa_cl;
  365. m.f.f_a.y = g->sf_y + (position / n_col) * sa_rl;
  366. m.f.f_a.rl = sa_rl;
  367. m.f.f_a.cl = sa_cl;
  368. insertNode(l, m);
  369. }
  370. else {
  371. int n_col = sf_cl / sa_cl;
  372. m.type = MASKEDAREA;
  373. m.f.f_ma.aid = i;
  374. m.f.f_a.x = g->sf_x + (position % n_col) * sa_cl;
  375. m.f.f_a.y = g->sf_y + (position / n_col) * sa_rl;
  376. m.f.f_ma.rl = sa_rl;
  377. m.f.f_ma.cl = sa_cl;
  378. strcpy(m.f.f_ma.mask, g->maskname);
  379. insertNode(l, m);
  380. }
  381. }
  382. }
  383. return NORMAL;
  384. }
  385. else if (strcmp(token, "SYSTEMATICCONTIGUOUS") == 0) {
  386. g->dist = 0;
  387. g->add_row = g->rl;
  388. g->add_col = g->cl;
  389. return GEN;
  390. }
  391. else if (strcmp(token, "SYSTEMATICNONCONTIGUOUS") == 0) {
  392. int dist;
  393. dist = atoi(strtok(NULL, "\n"));
  394. g->dist = dist;
  395. g->add_row = g->rl + dist;
  396. g->add_col = g->cl + dist;
  397. g->x = g->sf_x + dist;
  398. g->y = g->sf_y + dist;
  399. return GEN;
  400. }
  401. else if (strcmp(token, "STRATIFIEDRANDOM") == 0) {
  402. int r_strat, c_strat, r_strat_len, c_strat_len, loop, i;
  403. r_strat = atoi(strtok(NULL, "|"));
  404. c_strat = atoi(strtok(NULL, "\n"));
  405. r_strat_len = (int)rint(g->rows / r_strat);
  406. c_strat_len = (int)rint(g->cols / c_strat);
  407. if (r_strat_len < g->rl || c_strat_len < g->cl)
  408. G_fatal_error(_("Too many strats for raster map"));
  409. loop = r_strat * c_strat;
  410. srandom(0);
  411. for (i = 0; i < loop; i++) {
  412. msg m;
  413. if (g->maskname == NULL) {
  414. m.type = AREA;
  415. m.f.f_a.aid = i;
  416. m.f.f_a.x = (int)g->sf_x + ((i % c_strat) * c_strat_len) +
  417. (random() % (c_strat_len - g->cl));
  418. m.f.f_a.y = (int)g->sf_y + (rint(i / c_strat) * r_strat_len) +
  419. (random() % (r_strat_len - g->rl));
  420. m.f.f_a.rl = g->rl;
  421. m.f.f_a.cl = g->cl;
  422. insertNode(l, m);
  423. }
  424. else {
  425. m.type = MASKEDAREA;
  426. m.f.f_ma.aid = i;
  427. m.f.f_ma.x = (int)g->sf_x + ((i % c_strat) * c_strat_len) +
  428. (random() % (c_strat_len - g->cl));
  429. m.f.f_ma.y =
  430. (int)g->sf_y + (rint(i / c_strat) * r_strat_len) +
  431. (random() % (r_strat_len - g->rl));
  432. m.f.f_ma.rl = g->rl;
  433. m.f.f_ma.cl = g->cl;
  434. strcpy(m.f.f_ma.mask, g->maskname);
  435. insertNode(l, m);
  436. }
  437. }
  438. return NORMAL;
  439. }
  440. else {
  441. G_fatal_error(_("Illegal areas disposition"));
  442. return NORMAL;
  443. }
  444. return ERROR;
  445. }
  446. int next_Area(int parsed, list l, g_areas g, msg * m)
  447. {
  448. if (parsed == NORMAL) {
  449. if (l->size == 0)
  450. return 0;
  451. else {
  452. msg tmp;
  453. memcpy(&tmp, l->head->m, sizeof(msg));
  454. *m = tmp;
  455. removeNode(l);
  456. return 1;
  457. }
  458. }
  459. else {
  460. return next(g, m);
  461. }
  462. }
  463. int print_Output(int out, msg m)
  464. {
  465. if (m.type != DONE)
  466. return 0;
  467. else {
  468. char s[100];
  469. int len;
  470. sprintf(s, "RESULT %i|%f\n", m.f.f_d.aid, m.f.f_d.res);
  471. len = strlen(s);
  472. if (write(out, s, len) == len)
  473. return 1;
  474. else
  475. return 0;
  476. }
  477. }
  478. int error_Output(int out, msg m)
  479. {
  480. if (m.type != ERROR)
  481. return 0;
  482. else {
  483. char s[100];
  484. sprintf(s, "ERROR %i", m.f.f_d.aid);
  485. if (write(out, s, strlen(s)) == strlen(s))
  486. return 1;
  487. else
  488. return 0;
  489. }
  490. }
  491. int raster_Output(int fd, int aid, g_areas g, double res)
  492. {
  493. double toPut = res;
  494. off_t offset = (off_t) aid * sizeof(double);
  495. if (lseek(fd, offset, SEEK_SET) != offset) {
  496. G_message(_("Cannot make lseek"));
  497. return -1;
  498. }
  499. if (write(fd, &toPut, sizeof(double)) == 0)
  500. return 1;
  501. else
  502. return 0;
  503. }
  504. int write_raster(int mv_fd, int random_access, g_areas g)
  505. {
  506. int i = 0, j = 0, letti = 0;
  507. double *file_buf;
  508. DCELL *cell_buf;
  509. int cols, rows, center;
  510. cols = g->cols;
  511. rows = g->rows;
  512. center = g->sf_x + ((int)g->cl / 2);
  513. file_buf = G_malloc(cols * sizeof(double));
  514. lseek(random_access, 0, SEEK_SET);
  515. cell_buf = Rast_allocate_d_buf();
  516. Rast_set_d_null_value(cell_buf, Rast_window_cols() + 1);
  517. for (i = 0; i < g->sf_y + ((int)g->rl / 2); i++) {
  518. Rast_put_row(mv_fd, cell_buf, DCELL_TYPE);
  519. }
  520. for (i = 0; i < rows; i++) {
  521. letti = read(random_access, file_buf, (cols * sizeof(double)));
  522. if (letti == -1)
  523. G_message("%s", strerror(errno));
  524. for (j = 0; j < cols; j++) {
  525. cell_buf[j + center] = file_buf[j];
  526. }
  527. Rast_put_row(mv_fd, cell_buf, DCELL_TYPE);
  528. }
  529. Rast_set_d_null_value(cell_buf, Rast_window_cols() + 1);
  530. for (i = 0; i < Rast_window_rows() - g->sf_y - g->rows; i++)
  531. Rast_put_row(mv_fd, cell_buf, DCELL_TYPE);
  532. return 1;
  533. }