starpu_top.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 William Braik, Yann Courtois, Jean-Marie Couteyen, Anthony Roy
  4. * Copyright (C) 2011, 2012, 2013 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu_top.h>
  18. #include <top/starpu_top_message_queue.h>
  19. #include <top/starpu_top_connection.h>
  20. #include <top/starpu_top_core.h>
  21. #include <profiling/profiling.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <math.h>
  25. #include <common/timing.h>
  26. #include <common/thread.h>
  27. #include <common/config.h>
  28. extern struct _starpu_top_message_queue* _starpu_top_mt;
  29. int _starpu_top = 0;
  30. int starpu_top_debug_on = 0;
  31. unsigned int starpu_top_data_cpt = 0;
  32. unsigned int starpu_top_param_cpt = 0;
  33. struct starpu_top_data* starpu_top_first_data = NULL;
  34. struct starpu_top_param* starpu_top_first_param = NULL;
  35. struct starpu_top_data** starpu_top_datas;
  36. struct starpu_top_param** starpu_top_params;
  37. sem_t starpu_top_wait_for_go;
  38. starpu_pthread_mutex_t starpu_top_wait_for_continue_mutex;
  39. starpu_pthread_cond_t starpu_top_wait_for_continue_cond = STARPU_PTHREAD_COND_INITIALIZER;
  40. #undef _starpu_top_status_get
  41. int _starpu_top_status_get(void)
  42. {
  43. return _starpu_top;
  44. }
  45. static
  46. unsigned long long current_timestamp(void);
  47. /*********************************************
  48. *****************INIT FUNC********************
  49. **********************************************/
  50. static
  51. char *message_for_topdata_init(struct starpu_top_data* data);
  52. static
  53. char *message_for_topparam_init(struct starpu_top_param* param);
  54. /*
  55. * we store data and param in a tab to offer a O(1) access when the program is
  56. * running
  57. */
  58. static
  59. void copy_data_and_param(void)
  60. {
  61. printf("%s:%d trace\n", __FILE__, __LINE__);
  62. //copying datas
  63. starpu_top_datas = (struct starpu_top_data **) malloc(starpu_top_data_cpt*sizeof(struct starpu_top_data*));
  64. struct starpu_top_data* cur = starpu_top_first_data;
  65. unsigned int i = 0;
  66. for(i = 0; i < starpu_top_data_cpt; i++)
  67. {
  68. starpu_top_datas[i] = cur;
  69. cur = cur->next;
  70. }
  71. //copying params
  72. starpu_top_params = (struct starpu_top_param **) malloc(starpu_top_param_cpt*sizeof(struct starpu_top_param*));
  73. struct starpu_top_param* cur2 = starpu_top_first_param;
  74. for(i = 0; i < starpu_top_param_cpt; i++)
  75. {
  76. starpu_top_params[i] = cur2;
  77. cur2 = cur2->next;
  78. }
  79. }
  80. static void starpu_top_get_device_type(int id, char* type)
  81. {
  82. enum starpu_worker_archtype device_type=starpu_worker_get_type(id);
  83. switch (device_type)
  84. {
  85. case STARPU_CPU_WORKER:
  86. strncpy(type, "CPU",9);
  87. break;
  88. case STARPU_CUDA_WORKER:
  89. strncpy(type, "CUDA",9);
  90. break;
  91. case STARPU_OPENCL_WORKER:
  92. strncpy(type, "OPENCL",9);
  93. break;
  94. case STARPU_ANY_WORKER:
  95. strncpy(type, "ANY",9);
  96. break;
  97. case STARPU_MIC_WORKER:
  98. strncpy(type, "MIC", 9);
  99. break;
  100. case STARPU_SCC_WORKER:
  101. strncpy(type, "SCC", 9);
  102. break;
  103. }
  104. }
  105. static void starpu_top_send_devices_info(void)
  106. {
  107. char* message=(char *)malloc(5*sizeof(char));
  108. snprintf(message,5,"DEV\n");
  109. _starpu_top_message_add(_starpu_top_mt,message);
  110. unsigned int i;
  111. for(i=0;i<starpu_worker_get_count();i++)
  112. {
  113. message=(char *)malloc(sizeof(char)*128);
  114. char dev_type[10];
  115. char dev_name[64];
  116. starpu_top_get_device_type(i,dev_type);
  117. starpu_worker_get_name(i, dev_name,64);
  118. snprintf(message, 128, "%u;%s;%s\n", i, dev_type, dev_name);
  119. _starpu_top_message_add(_starpu_top_mt,message);
  120. }
  121. message=(char*)malloc(6*sizeof(char));
  122. snprintf(message,6,"/DEV\n");
  123. _starpu_top_message_add(_starpu_top_mt,message);
  124. }
  125. void starpu_top_init_and_wait(const char* server_name)
  126. {
  127. _starpu_top=1;
  128. sem_init(&starpu_top_wait_for_go,0,0);
  129. STARPU_PTHREAD_MUTEX_INIT(&starpu_top_wait_for_continue_mutex, NULL);
  130. //profiling activation
  131. starpu_profiling_status_set(STARPU_PROFILING_ENABLE);
  132. //init locked queue before adding the first message
  133. _starpu_top_mt = _starpu_top_message_queue_new();
  134. STARPU_ASSERT(_starpu_top_mt);
  135. //waiting for UI to connect
  136. printf("%s:%d launching network threads\n", __FILE__, __LINE__);
  137. _starpu_top_communications_threads_launcher();
  138. //sending server information (report to protocol)
  139. char* message = (char *) malloc(strlen("SERVERINFO\n")+1);
  140. sprintf(message, "%s", "SERVERINFO\n");
  141. _starpu_top_message_add(_starpu_top_mt,message);
  142. message = (char *) malloc(strlen(server_name)+2);
  143. sprintf(message, "%s\n", server_name);
  144. _starpu_top_message_add(_starpu_top_mt,message);
  145. message = (char *) malloc(25);
  146. sprintf(message, "%llu\n", current_timestamp());
  147. _starpu_top_message_add(_starpu_top_mt,message);
  148. message = (char *) malloc(strlen("/SERVERINFO\n")+1);
  149. sprintf(message,"%s", "/SERVERINFO\n");
  150. _starpu_top_message_add(_starpu_top_mt,message);
  151. //sending data list
  152. message = (char *) malloc(strlen("DATA\n")+1);
  153. sprintf(message, "%s", "DATA\n");
  154. _starpu_top_message_add(_starpu_top_mt,message);
  155. struct starpu_top_data * cur_data = starpu_top_first_data;
  156. while(cur_data != NULL)
  157. {
  158. _starpu_top_message_add(_starpu_top_mt,message_for_topdata_init(cur_data));
  159. cur_data = cur_data->next;
  160. }
  161. message = (char *) malloc(strlen("/DATA\n")+1);
  162. sprintf(message, "%s", "/DATA\n");
  163. _starpu_top_message_add(_starpu_top_mt,message);
  164. //sending parameter list
  165. message = (char *) malloc(strlen("PARAMS\n")+1);
  166. sprintf(message, "%s", "PARAMS\n");
  167. _starpu_top_message_add(_starpu_top_mt,message);
  168. struct starpu_top_param * cur_param = starpu_top_first_param;
  169. printf("%s:%d sending parameters\n", __FILE__, __LINE__);
  170. while(cur_param != NULL)
  171. {
  172. _starpu_top_message_add(_starpu_top_mt,message_for_topparam_init(cur_param));
  173. cur_param = cur_param->next;
  174. }
  175. printf("%s:%d parameters sended\n", __FILE__, __LINE__);
  176. message = (char *) malloc(strlen("/PARAMS\n")+1);
  177. sprintf(message, "%s", "/PARAMS\n");
  178. _starpu_top_message_add(_starpu_top_mt,message);
  179. //sending DEVICE list
  180. printf("%s:%d sending devices info\n", __FILE__, __LINE__);
  181. starpu_top_send_devices_info();
  182. printf("%s:%d devices_info sended\n", __FILE__, __LINE__);
  183. //copying data and params
  184. copy_data_and_param();
  185. //sending READY message
  186. message = (char *) malloc(strlen("READY\n")+1);
  187. sprintf(message, "%s", "READY\n");
  188. _starpu_top_message_add(_starpu_top_mt,message);
  189. //This threads keeps locked while we don't receive an GO message from UI
  190. printf("%s:%d waiting for GO message\n", __FILE__, __LINE__);
  191. sem_wait(&starpu_top_wait_for_go);
  192. }
  193. static
  194. void starpu_top_enqueue_data(struct starpu_top_data * data)
  195. {
  196. if(starpu_top_first_data == NULL)
  197. {
  198. starpu_top_first_data = data;
  199. }
  200. else
  201. {
  202. struct starpu_top_data * cur = starpu_top_first_data;
  203. while(cur->next != NULL)
  204. cur = cur->next;
  205. cur->next = data;
  206. }
  207. }
  208. struct starpu_top_data * starpu_top_add_data_boolean(const char* data_name,
  209. int active)
  210. {
  211. struct starpu_top_data * data = (struct starpu_top_data *) malloc(sizeof(struct starpu_top_data));
  212. data->id = starpu_top_data_cpt++;
  213. data->name = data_name;
  214. data->type = STARPU_TOP_DATA_BOOLEAN;
  215. data->active = active;
  216. data->next = NULL;
  217. starpu_top_enqueue_data(data);
  218. return data;
  219. }
  220. struct starpu_top_data * starpu_top_add_data_integer(const char* data_name,
  221. int minimum_value,
  222. int maximum_value,
  223. int active)
  224. {
  225. struct starpu_top_data * data = (struct starpu_top_data *) malloc(sizeof(struct starpu_top_data));
  226. data->id = starpu_top_data_cpt++;
  227. data->name = data_name;
  228. data->type = STARPU_TOP_DATA_INTEGER;
  229. data->int_min_value = minimum_value;
  230. data->int_max_value = maximum_value;
  231. data->active = active;
  232. data->next = NULL;
  233. starpu_top_enqueue_data(data);
  234. return data;
  235. }
  236. struct starpu_top_data* starpu_top_add_data_float(const char* data_name,
  237. double minimum_value,
  238. double maximum_value,
  239. int active)
  240. {
  241. struct starpu_top_data * data = (struct starpu_top_data *) malloc(sizeof(struct starpu_top_data));
  242. data->id = starpu_top_data_cpt++;
  243. data->name = data_name;
  244. data->type = STARPU_TOP_DATA_FLOAT;
  245. data->double_min_value = minimum_value;
  246. data->double_max_value = maximum_value;
  247. data->active = active;
  248. data->next = NULL;
  249. starpu_top_enqueue_data(data);
  250. return data;
  251. }
  252. char *message_for_topdata_init(struct starpu_top_data* data)
  253. {
  254. char*message = (char *) malloc(256+strlen(data->name));
  255. switch(data->type)
  256. {
  257. case STARPU_TOP_DATA_BOOLEAN:
  258. sprintf(message,
  259. "BOOL;%u;%s;%d\n",
  260. data->id,
  261. data->name,
  262. data->active ? 1 : 0);
  263. break;
  264. case STARPU_TOP_DATA_INTEGER:
  265. sprintf(message,
  266. "INT;%u;%s;%d;%d;%d\n",
  267. data->id,
  268. data->name,
  269. data->int_min_value,
  270. data->int_max_value,
  271. data->active ? 1 : 0);
  272. break;
  273. case STARPU_TOP_DATA_FLOAT:
  274. sprintf(message,
  275. "FLOAT;%u;%s;%f;%f;%d\n",
  276. data->id,
  277. data->name,
  278. data->double_min_value,
  279. data->double_max_value,
  280. data->active ? 1 : 0);
  281. break;
  282. }
  283. return message;
  284. }
  285. char *message_for_topparam_init(struct starpu_top_param* param)
  286. {
  287. char*message = NULL;
  288. int i;
  289. int length=0;
  290. switch(param->type)
  291. {
  292. case STARPU_TOP_PARAM_BOOLEAN:
  293. message = (char *) malloc(256);
  294. sprintf(message,
  295. "BOOL;%u;%s;%d\n",
  296. param->id,
  297. param->name,
  298. (*(int*)(param->value)) ? 1 : 0);
  299. break;
  300. case STARPU_TOP_PARAM_INTEGER:
  301. message = (char *) malloc(256);
  302. sprintf(message,
  303. "INT;%u;%s;%d;%d;%d\n",param->id,
  304. param->name,
  305. param->int_min_value,
  306. param->int_max_value,
  307. *(int*)(param->value));
  308. break;
  309. case STARPU_TOP_PARAM_FLOAT:
  310. message = (char *) malloc(256);
  311. sprintf(message,
  312. "FLOAT;%u;%s;%f;%f;%f\n",
  313. param->id,
  314. param->name,
  315. param->double_min_value,
  316. param->double_max_value,
  317. *(double*)(param->value));
  318. break;
  319. case STARPU_TOP_PARAM_ENUM:
  320. //compute message lenght
  321. for(i = 0; i < param->nb_values; i++)
  322. {
  323. length += strlen(param->enum_values[i])+1;
  324. }
  325. message = (char *) malloc(256+length);
  326. sprintf(message,
  327. "ENUM;%u;%s;",
  328. param->id,
  329. param->name);
  330. //compute the begin of enums elements in message
  331. char* cur = message+strlen(message);
  332. //add each enum element
  333. for(i = 0; i < param->nb_values; i++)
  334. {
  335. strcpy(cur, param->enum_values[i]);
  336. cur+=strlen(cur);
  337. *cur=';';
  338. cur++;
  339. }
  340. sprintf(cur,
  341. "%d\n",
  342. *((int*)(param->value)));
  343. break;
  344. }
  345. return message;
  346. }
  347. static
  348. void starpu_top_enqueue_param(struct starpu_top_param* param)
  349. {
  350. if(starpu_top_first_param == NULL)
  351. {
  352. starpu_top_first_param = param;
  353. }
  354. else
  355. {
  356. struct starpu_top_param * cur = starpu_top_first_param;
  357. while(cur->next != NULL)
  358. cur = cur->next;
  359. cur->next = param;
  360. }
  361. }
  362. struct starpu_top_param* starpu_top_register_parameter_boolean(const char* param_name,
  363. int* parameter_field,
  364. void (*callback)(struct starpu_top_param*))
  365. {
  366. STARPU_ASSERT(!_starpu_top_status_get());
  367. struct starpu_top_param * param = (struct starpu_top_param *) malloc(sizeof(struct starpu_top_param));
  368. param->callback = callback;
  369. param->name = param_name;
  370. param->id = starpu_top_param_cpt++;
  371. param->type = STARPU_TOP_PARAM_BOOLEAN;
  372. param->value = (void*)parameter_field;
  373. param->next = NULL;
  374. starpu_top_enqueue_param(param);
  375. return param;
  376. }
  377. struct starpu_top_param* starpu_top_register_parameter_integer(const char* param_name,
  378. int* parameter_field,
  379. int minimum_value,
  380. int maximum_value,
  381. void (*callback)(struct starpu_top_param*))
  382. {
  383. STARPU_ASSERT(!_starpu_top_status_get());
  384. struct starpu_top_param * param = (struct starpu_top_param *) malloc(sizeof(struct starpu_top_param));
  385. param->callback = callback;
  386. param->name = param_name;
  387. param->id = starpu_top_param_cpt++;
  388. param->type = STARPU_TOP_PARAM_INTEGER;
  389. param->value = (void*)parameter_field;
  390. param->int_min_value = minimum_value;
  391. param->int_max_value = maximum_value;
  392. param->next = NULL;
  393. starpu_top_enqueue_param(param);
  394. return param;
  395. }
  396. struct starpu_top_param* starpu_top_register_parameter_float(const char* param_name,
  397. double* parameter_field,
  398. double minimum_value,
  399. double maximum_value,
  400. void (*callback)(struct starpu_top_param*))
  401. {
  402. STARPU_ASSERT(!_starpu_top_status_get());
  403. struct starpu_top_param * param = (struct starpu_top_param *) malloc(sizeof(struct starpu_top_param));
  404. param->callback = callback;
  405. param->name = param_name;
  406. param->id = starpu_top_param_cpt++;
  407. param->type = STARPU_TOP_PARAM_FLOAT;
  408. param->value = (void*)parameter_field;
  409. param->double_min_value = minimum_value;
  410. param->double_max_value = maximum_value;
  411. param->next = NULL;
  412. starpu_top_enqueue_param(param);
  413. return param;
  414. }
  415. struct starpu_top_param* starpu_top_register_parameter_enum(const char* param_name,
  416. int* parameter_field,
  417. char** values,
  418. int nb_values,
  419. void (*callback)(struct starpu_top_param*))
  420. {
  421. STARPU_ASSERT(!_starpu_top_status_get());
  422. struct starpu_top_param * param = (struct starpu_top_param *) malloc(sizeof(struct starpu_top_param));
  423. param->callback = callback;
  424. param->name = param_name;
  425. param->id = starpu_top_param_cpt++;
  426. param->type = STARPU_TOP_PARAM_ENUM;
  427. param->value = (void*)parameter_field;
  428. param->enum_values = values;
  429. param->nb_values = nb_values;
  430. param->next = NULL;
  431. starpu_top_enqueue_param(param);
  432. return param;
  433. }
  434. /*********************************************
  435. *****************UPDATE FUNC******************
  436. **********************************************/
  437. void starpu_top_update_data_boolean(const struct starpu_top_data* data, int value)
  438. {
  439. if (!_starpu_top_status_get())
  440. return;
  441. if(data->active)
  442. {
  443. char*message = (char *) malloc(256+strlen(data->name));
  444. sprintf(message,
  445. "U;%u;%d;%llu\n",
  446. data->id,
  447. (value?1:0),
  448. current_timestamp());
  449. _starpu_top_message_add(_starpu_top_mt,message);
  450. }
  451. }
  452. void starpu_top_update_data_integer(const struct starpu_top_data* data, int value)
  453. {
  454. if (!_starpu_top_status_get())
  455. return;
  456. if(data->active)
  457. {
  458. char*message = (char *) malloc(256+strlen(data->name));
  459. sprintf(message,
  460. "U;%u;%d;%llu\n",
  461. data->id,
  462. value,
  463. current_timestamp());
  464. _starpu_top_message_add(_starpu_top_mt,message);
  465. }
  466. }
  467. void starpu_top_update_data_float(const struct starpu_top_data* data, double value)
  468. {
  469. if (!_starpu_top_status_get())
  470. return;
  471. if(data->active)
  472. {
  473. char*message = (char *) malloc(256+strlen(data->name));
  474. sprintf(message,
  475. "U;%u;%f;%llu\n",
  476. data->id, value,
  477. current_timestamp());
  478. _starpu_top_message_add(_starpu_top_mt,message);
  479. }
  480. }
  481. void starpu_top_update_parameter(const struct starpu_top_param* param)
  482. {
  483. if (!_starpu_top_status_get())
  484. return;
  485. char*message = (char *) malloc(50);
  486. switch(param->type)
  487. {
  488. case STARPU_TOP_PARAM_BOOLEAN:
  489. case STARPU_TOP_PARAM_INTEGER:
  490. case STARPU_TOP_PARAM_ENUM:
  491. sprintf(message,
  492. "SET;%u;%d;%llu\n",
  493. param->id,
  494. *((int*)param->value),
  495. current_timestamp());
  496. break;
  497. case STARPU_TOP_PARAM_FLOAT:
  498. sprintf(message,
  499. "SET;%u;%f;%llu\n",
  500. param->id,
  501. *((double*)param->value),
  502. current_timestamp());
  503. break;
  504. }
  505. _starpu_top_message_add(_starpu_top_mt,message);
  506. }
  507. /*********************************************
  508. *****************DEBUG FUNC******************
  509. **********************************************/
  510. void starpu_top_debug_log(const char* debug_message)
  511. {
  512. if(starpu_top_debug_on)
  513. {
  514. //length can be up to strlen*2, if message contains only unwanted chars
  515. char * message = (char *) malloc(strlen(debug_message)*2+16);
  516. sprintf(message,"MESSAGE;");
  517. //escape unwanted char : ; and \n
  518. char* cur = message+8;
  519. while(*debug_message!='\0')
  520. {
  521. if(*debug_message=='\n' || *debug_message==';')
  522. {
  523. *cur='\\';
  524. cur++;
  525. }
  526. *cur = *debug_message;
  527. cur++;
  528. debug_message++;
  529. }
  530. *cur='\n';
  531. cur++;
  532. *cur='\0';
  533. _starpu_top_message_add(_starpu_top_mt,message);
  534. }
  535. }
  536. void starpu_top_debug_lock(const char* debug_message)
  537. {
  538. if(starpu_top_debug_on)
  539. {
  540. char * message = (char *) malloc(strlen(debug_message)*2+16);
  541. sprintf(message,"LOCK;");
  542. char* cur = message+5;
  543. while(*debug_message!='\0')
  544. {
  545. if(*debug_message=='\n' || *debug_message==';')
  546. {
  547. *cur='\\';
  548. cur++;
  549. }
  550. *cur = *debug_message;
  551. cur++;
  552. debug_message++;
  553. }
  554. *cur='\n';
  555. *(cur+1)='\0';
  556. _starpu_top_message_add(_starpu_top_mt,message);
  557. //This threads keeps locked while we don't receive an STEP message
  558. STARPU_PTHREAD_MUTEX_LOCK(&starpu_top_wait_for_continue_mutex);
  559. STARPU_PTHREAD_COND_WAIT(&starpu_top_wait_for_continue_cond,
  560. &starpu_top_wait_for_continue_mutex);
  561. STARPU_PTHREAD_MUTEX_UNLOCK(&starpu_top_wait_for_continue_mutex);
  562. }
  563. }
  564. /********************************************
  565. **************TIME FUNCTION****************
  566. *******************************************/
  567. unsigned long long current_timestamp(void)
  568. {
  569. struct timespec now;
  570. _starpu_clock_gettime(&now);
  571. return _starpu_top_timing_timespec_to_ms(&now);
  572. }
  573. unsigned long long _starpu_top_timing_timespec_to_ms(const struct timespec *ts)
  574. {
  575. return (1000.0*ts->tv_sec) + (0.000001*ts->tv_nsec);
  576. }
  577. /********************************************
  578. **************INPUT PROCESSING**************
  579. *******************************************/
  580. static
  581. enum starpu_top_message_type starpu_top_get_message_type(const char* message)
  582. {
  583. if(!strncmp("GO\n", message,3))
  584. return TOP_TYPE_GO;
  585. else if(!strncmp("SET;", message,4))
  586. return TOP_TYPE_SET;
  587. else if(!strncmp("STEP\n", message,9))
  588. return TOP_TYPE_CONTINUE;
  589. else if(!strncmp("ENABLE;", message,7))
  590. return TOP_TYPE_ENABLE;
  591. else if(!strncmp("DISABLE;", message,8))
  592. return TOP_TYPE_DISABLE;
  593. else if(!strncmp("DEBUG;", message,6))
  594. return TOP_TYPE_DEBUG;
  595. else
  596. return TOP_TYPE_UNKNOW;
  597. }
  598. static
  599. void starpu_top_unlock_starpu(void)
  600. {
  601. sem_post(&starpu_top_wait_for_go);
  602. printf("%s:%d starpu started\n", __FILE__, __LINE__);
  603. }
  604. static
  605. void starpu_top_change_data_active(char* message, int active)
  606. {
  607. char* debut = strstr(message, ";")+1;
  608. char* fin = strstr(debut+1, "\n");
  609. *fin = '\0';
  610. int data_id = atoi(debut);
  611. printf("%s:%d data %d %s\n", __FILE__, __LINE__, data_id, active ? "ENABLED" : "DISABLE");
  612. starpu_top_datas[data_id]->active = active;
  613. }
  614. static
  615. void starpu_top_change_parameter_value(const char* message)
  616. {
  617. const char*tmp = strstr(message, ";")+1;
  618. int param_id = atoi(tmp);
  619. struct starpu_top_param* param = starpu_top_params[param_id];
  620. tmp = strstr(tmp+1,";")+1;
  621. int* val_ptr_int;
  622. double* val_ptr_double;
  623. switch(param->type)
  624. {
  625. case STARPU_TOP_PARAM_BOOLEAN:
  626. case STARPU_TOP_PARAM_INTEGER:
  627. val_ptr_int = (int*)param->value;
  628. *val_ptr_int = atoi(tmp);
  629. break;
  630. case STARPU_TOP_PARAM_FLOAT:
  631. val_ptr_double = (double*)param->value;
  632. *val_ptr_double = atof(tmp);
  633. break;
  634. case STARPU_TOP_PARAM_ENUM:
  635. val_ptr_int = (int*)param->value;
  636. *val_ptr_int = atoi(tmp);
  637. break;
  638. }
  639. if(param->callback != NULL)
  640. param->callback(param);
  641. }
  642. static
  643. void starpu_top_change_debug_mode(const char*message)
  644. {
  645. const char* debut = strstr(message, ";")+1;
  646. if(!strncmp("ON",debut, 2))
  647. {
  648. starpu_top_debug_on = 1;
  649. printf("%s:%d debug is now ON\n", __FILE__, __LINE__);
  650. }
  651. else
  652. {
  653. starpu_top_debug_on = 0;
  654. printf("%s:%d debug is now OFF\n", __FILE__, __LINE__);
  655. }
  656. char * m = (char *) malloc(strlen(message)+1);
  657. sprintf(m,"%s",message);
  658. _starpu_top_message_add(_starpu_top_mt,m);
  659. }
  660. /*
  661. * Unlock starpu if it was locked in debug state
  662. */
  663. static
  664. void starpu_top_debug_next_step(void)
  665. {
  666. STARPU_PTHREAD_COND_SIGNAL(&starpu_top_wait_for_continue_cond);
  667. }
  668. void _starpu_top_process_input_message(char *buffer)
  669. {
  670. enum starpu_top_message_type message_type = starpu_top_get_message_type(buffer);
  671. switch(message_type)
  672. {
  673. case TOP_TYPE_GO:
  674. starpu_top_unlock_starpu();
  675. break;
  676. case TOP_TYPE_ENABLE:
  677. starpu_top_change_data_active(buffer, 1);
  678. break;
  679. case TOP_TYPE_DISABLE:
  680. starpu_top_change_data_active(buffer, 0);
  681. break;
  682. case TOP_TYPE_SET:
  683. starpu_top_change_parameter_value(buffer);
  684. break;
  685. case TOP_TYPE_DEBUG:
  686. starpu_top_change_debug_mode(buffer);
  687. break;
  688. case TOP_TYPE_CONTINUE:
  689. starpu_top_debug_next_step();
  690. break;
  691. default:
  692. printf("%s:%d unknow message : '%s'\n", __FILE__, __LINE__, buffer);
  693. }
  694. }