You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

658 lines
12 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "basicUtils.h"
  2. void free_cortexList(cortexList * l){
  3. for (cortexList::iterator index = l->begin(); index < l->end(); index++) free((*index));
  4. l->clear();
  5. delete(l);
  6. };
  7. double what_time_is_it_now(){
  8. #ifdef __linux__
  9. struct timespec now;
  10. clock_gettime(CLOCK_REALTIME, &now);
  11. return now.tv_sec + now.tv_nsec*1e-9;
  12. #else
  13. return 0;
  14. #endif
  15. };
  16. int *read_intlist(char *gpu_list, int *ngpus, int d)
  17. {
  18. int *gpus = 0;
  19. if (gpu_list){
  20. size_t len = strlen(gpu_list);
  21. *ngpus = 1;
  22. for (size_t i = 0; i < len; ++i){
  23. if (gpu_list[i] == ',')++*ngpus;
  24. };
  25. gpus = (int*)calloc(*ngpus, sizeof(int));
  26. for (int i = 0; i < *ngpus; ++i){
  27. gpus[i] = atoi(gpu_list);
  28. gpu_list = strchr(gpu_list, ',') + 1;
  29. };
  30. } else {
  31. gpus = (int*)calloc(1, sizeof(float));
  32. *gpus = d;
  33. *ngpus = 1;
  34. };
  35. return gpus;
  36. }
  37. uint32_t *read_map(char *filename){
  38. uint32_t n = 0;
  39. uint32_t *map = 0;
  40. char *str;
  41. FILE *file = fopen(filename, "r");
  42. if (!file) file_error(filename);
  43. while ((str = fgetl(file))){
  44. ++n;
  45. map = (uint32_t*)realloc(map, n*sizeof(uint32_t));
  46. map[n - 1] = atoi(str);
  47. };
  48. return map;
  49. };
  50. void sorta_shuffle(void *arr, size_t n, size_t size, size_t sections)
  51. {
  52. size_t i;
  53. for (i = 0; i < sections; ++i){
  54. size_t start = n*i / sections;
  55. size_t end = n*(i + 1) / sections;
  56. size_t num = end - start;
  57. shuffle((void *)((size_t)arr + start*size), num, size);
  58. }
  59. }
  60. void shuffle(void *arr, size_t n, size_t size)
  61. {
  62. size_t i;
  63. void *swp = calloc(1, size);
  64. for (i = 0; i < n - 1; ++i){
  65. size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
  66. memcpy(swp, (void *)((size_t)arr + j*size), size);
  67. memcpy((void *)((size_t)arr + j*size), (void *)((size_t)arr + i*size), size);
  68. memcpy((void *)((size_t)arr + i*size), swp, size);
  69. }
  70. }
  71. void del_arg(int argc, char **argv, int index)
  72. {
  73. int i;
  74. for (i = index; i < argc - 1; ++i) argv[i] = argv[i + 1];
  75. argv[i] = 0;
  76. }
  77. int find_arg(int argc, char* argv[], char *arg)
  78. {
  79. int i;
  80. for (i = 0; i < argc; ++i) {
  81. if (!argv[i]) continue;
  82. if (0 == strcmp(argv[i], arg)) {
  83. del_arg(argc, argv, i);
  84. return 1;
  85. }
  86. }
  87. return 0;
  88. }
  89. int find_int_arg(int argc, char **argv, char *arg, int def)
  90. {
  91. int i;
  92. for (i = 0; i < argc - 1; ++i){
  93. if (!argv[i]) continue;
  94. if (0 == strcmp(argv[i], arg)){
  95. def = atoi(argv[i + 1]);
  96. del_arg(argc, argv, i);
  97. del_arg(argc, argv, i);
  98. break;
  99. }
  100. }
  101. return def;
  102. }
  103. float find_float_arg(int argc, char **argv, char *arg, float def)
  104. {
  105. int i;
  106. for (i = 0; i < argc - 1; ++i){
  107. if (!argv[i]) continue;
  108. if (0 == strcmp(argv[i], arg)){
  109. def = (float)atof(argv[i + 1]);
  110. del_arg(argc, argv, i);
  111. del_arg(argc, argv, i);
  112. break;
  113. }
  114. }
  115. return def;
  116. }
  117. char *find_char_arg(int argc, char **argv, char *arg, char *def)
  118. {
  119. int i;
  120. for (i = 0; i < argc - 1; ++i){
  121. if (!argv[i]) continue;
  122. if (0 == strcmp(argv[i], arg)){
  123. def = argv[i + 1];
  124. del_arg(argc, argv, i);
  125. del_arg(argc, argv, i);
  126. break;
  127. }
  128. }
  129. return def;
  130. }
  131. char *basecfg(char *cfgfile)
  132. {
  133. char *c = cfgfile;
  134. char *next;
  135. while ((next = strchr(c, '/')))
  136. {
  137. c = next + 1;
  138. }
  139. c = copy_string(c);
  140. next = strchr(c, '.');
  141. if (next) *next = 0;
  142. return c;
  143. }
  144. int alphanum_to_int(char c)
  145. {
  146. return (c < 58) ? c - 48 : c - 87;
  147. }
  148. char int_to_alphanum(int i)
  149. {
  150. if (i == 36) return '.';
  151. return (i < 10) ? i + 48 : i + 87;
  152. }
  153. void pm(int M, int N, float *A)
  154. {
  155. int i, j;
  156. for (i = 0; i < M; ++i){
  157. printf("%d ", i + 1);
  158. for (j = 0; j < N; ++j){
  159. printf("%2.4f, ", A[i*N + j]);
  160. }
  161. printf("\n");
  162. }
  163. printf("\n");
  164. }
  165. void find_replace(char *str, char *orig, char *rep, char *output)
  166. {
  167. char buffer[4096] = { 0 };
  168. char *p;
  169. sprintf(buffer, "%s", str);
  170. if (!(p = strstr(buffer, orig))){ // Is 'orig' even in 'str'?
  171. sprintf(output, "%s", str);
  172. return;
  173. }
  174. *p = '\0';
  175. sprintf(output, "%s%s%s", buffer, rep, p + strlen(orig));
  176. }
  177. float sec(clock_t clocks)
  178. {
  179. return (float)clocks / CLOCKS_PER_SEC;
  180. }
  181. void top_k(float *a, int n, int k, int *index)
  182. {
  183. int i, j;
  184. for (j = 0; j < k; ++j) index[j] = -1;
  185. for (i = 0; i < n; ++i){
  186. int curr = i;
  187. for (j = 0; j < k; ++j){
  188. if ((index[j] < 0) || a[curr] > a[index[j]]){
  189. int swap = curr;
  190. curr = index[j];
  191. index[j] = swap;
  192. }
  193. }
  194. }
  195. }
  196. void error(const char *s)
  197. {
  198. perror(s);
  199. assert(0);
  200. exit(-1);
  201. }
  202. void malloc_error()
  203. {
  204. fprintf(stderr, "Malloc error\n");
  205. exit(-1);
  206. }
  207. void file_error(char *s)
  208. {
  209. fprintf(stderr, "Couldn't open file: %s\n", s);
  210. exit(0);
  211. }
  212. cortexList *split_str(char *s, char delim){
  213. size_t i;
  214. size_t len = strlen(s);
  215. cortexList *RetVal;
  216. RetVal = new cortexList();
  217. //list_insert(l, s);
  218. RetVal->push_back(s);
  219. for (i = 0; i < len; ++i){
  220. if (s[i] == delim){
  221. s[i] = '\0';
  222. //list_insert(l, &(s[i + 1]));
  223. RetVal->push_back(&(s[i + 1]));
  224. };
  225. };
  226. return RetVal;
  227. };
  228. void strip(char *s)
  229. {
  230. size_t i;
  231. size_t len = strlen(s);
  232. size_t offset = 0;
  233. for (i = 0; i < len; ++i){
  234. char c = s[i];
  235. if (c == ' ' || c == '\t' || c == '\n') ++offset;
  236. else s[i - offset] = c;
  237. }
  238. s[len - offset] = '\0';
  239. }
  240. void strip_char(char *s, char bad)
  241. {
  242. size_t i;
  243. size_t len = strlen(s);
  244. size_t offset = 0;
  245. for (i = 0; i < len; ++i){
  246. char c = s[i];
  247. if (c == bad) ++offset;
  248. else s[i - offset] = c;
  249. }
  250. s[len - offset] = '\0';
  251. }
  252. void free_ptrs(void **ptrs, int n)
  253. {
  254. int i;
  255. for (i = 0; i < n; ++i) free(ptrs[i]);
  256. free(ptrs);
  257. }
  258. char *fgetl(FILE *fp)
  259. {
  260. if (feof(fp)) return 0;
  261. size_t size;
  262. size = 512;
  263. char *line = (char*)malloc(size*sizeof(char));
  264. if (!fgets(line, (int)size, fp)){
  265. free(line);
  266. return 0;
  267. }
  268. size_t curr = strlen(line);
  269. while ((line[curr - 1] != '\n') && !feof(fp)){
  270. if (curr == size - 1){
  271. size *= 2;
  272. line = (char*)realloc(line, size*sizeof(char));
  273. if (!line) {
  274. printf("%ld\n", size);
  275. malloc_error();
  276. }
  277. }
  278. size_t readsize = size - curr;
  279. if (readsize > INT_MAX) readsize = INT_MAX - 1;
  280. fgets(&line[curr], (int) readsize, fp);
  281. curr = strlen(line);
  282. }
  283. if (line[curr - 1] == '\n') line[curr - 1] = '\0';
  284. return line;
  285. }
  286. int read_int(int fd)
  287. {
  288. int n = 0;
  289. int next = _read(fd, &n, sizeof(int));
  290. if (next <= 0) return -1;
  291. return n;
  292. }
  293. void write_int(int fd, int n)
  294. {
  295. int next = _write(fd, &n, sizeof(int));
  296. if (next <= 0) error("read failed");
  297. }
  298. int read_all_fail(int fd, char *buffer, size_t bytes)
  299. {
  300. size_t n = 0;
  301. while (n < bytes){
  302. int next = _read(fd, buffer + n, (unsigned int)(bytes - n));
  303. if (next <= 0) return 1;
  304. n += next;
  305. }
  306. return 0;
  307. }
  308. int write_all_fail(int fd, char *buffer, size_t bytes)
  309. {
  310. size_t n = 0;
  311. while (n < bytes){
  312. size_t next = _write(fd, buffer + n, (unsigned int)(bytes - n));
  313. if (next <= 0) return 1;
  314. n += next;
  315. }
  316. return 0;
  317. }
  318. void read_all(int fd, char *buffer, size_t bytes)
  319. {
  320. size_t n = 0;
  321. while (n < bytes){
  322. int next = _read(fd, buffer + n, (unsigned int)(bytes - n));
  323. if (next <= 0) error("read failed");
  324. n += next;
  325. }
  326. }
  327. void write_all(int fd, char *buffer, size_t bytes)
  328. {
  329. size_t n = 0;
  330. while (n < bytes){
  331. size_t next = _write(fd, buffer + n, (unsigned int)(bytes - n));
  332. if (next <= 0) error("write failed");
  333. n += next;
  334. }
  335. }
  336. char *copy_string(char *s)
  337. {
  338. char *copy = (char*)malloc(strlen(s) + 1);
  339. strncpy(copy, s, strlen(s) + 1);
  340. return copy;
  341. }
  342. cortexList *parse_csv_line(char *line){
  343. cortexList *RetVal;
  344. RetVal = new cortexList();
  345. char *c, *p;
  346. int in = 0;
  347. for (c = line, p = line; *c != '\0'; ++c){
  348. if (*c == '"') in = !in;
  349. else if (*c == ',' && !in){
  350. *c = '\0';
  351. //list_insert(l, copy_string(p));
  352. RetVal->push_back(copy_string(p));
  353. p = c + 1;
  354. }
  355. }
  356. //list_insert(l, copy_string(p));
  357. RetVal->push_back(copy_string(p));
  358. return RetVal;
  359. };
  360. int count_fields(char *line)
  361. {
  362. int count = 0;
  363. int done = 0;
  364. char *c;
  365. for (c = line; !done; ++c){
  366. done = (*c == '\0');
  367. if (*c == ',' || done) ++count;
  368. }
  369. return count;
  370. }
  371. float *parse_fields(char *line, int n)
  372. {
  373. float *field = (float*)calloc(n, sizeof(float));
  374. char *c, *p, *end;
  375. int count = 0;
  376. int done = 0;
  377. for (c = line, p = line; !done; ++c){
  378. done = (*c == '\0');
  379. if (*c == ',' || done){
  380. *c = '\0';
  381. field[count] = (float)strtod(p, &end);
  382. if (p == c) field[count] = (float)0;//nan("");
  383. if (end != c && (end != c - 1 || *end != '\r')) field[count] = (float)0;//nan(""); //DOS file formats!
  384. p = c + 1;
  385. ++count;
  386. }
  387. }
  388. return field;
  389. }
  390. float sum_array(float *a, int n)
  391. {
  392. int i;
  393. float sum = 0;
  394. for (i = 0; i < n; ++i) sum += a[i];
  395. return sum;
  396. }
  397. float mean_array(float *a, int n)
  398. {
  399. return sum_array(a, n) / n;
  400. }
  401. void mean_arrays(float **a, int n, int els, float *avg)
  402. {
  403. int i;
  404. int j;
  405. memset(avg, 0, els*sizeof(float));
  406. for (j = 0; j < n; ++j){
  407. for (i = 0; i < els; ++i){
  408. avg[i] += a[j][i];
  409. }
  410. }
  411. for (i = 0; i < els; ++i){
  412. avg[i] /= n;
  413. }
  414. }
  415. //void print_statistics(float *a, int n)
  416. //{
  417. // float m = mean_array(a, n);
  418. // float v = variance_array(a, n);
  419. // printf("MSE: %.6f, Mean: %.6f, Variance: %.6f\n", mse_array(a, n), m, v);
  420. //}
  421. float variance_array(float *a, int n)
  422. {
  423. int i;
  424. float sum = 0;
  425. float mean = mean_array(a, n);
  426. for (i = 0; i < n; ++i) sum += (a[i] - mean)*(a[i] - mean);
  427. float variance = sum / n;
  428. return variance;
  429. }
  430. int constrain_int(int a, int min, int max)
  431. {
  432. if (a < min) return min;
  433. if (a > max) return max;
  434. return a;
  435. }
  436. float constrain(float min, float max, float a)
  437. {
  438. if (a < min) return min;
  439. if (a > max) return max;
  440. return a;
  441. }
  442. float dist_array(float *a, float *b, int n, int sub)
  443. {
  444. int i;
  445. float sum = 0;
  446. for (i = 0; i < n; i += sub) sum += (float)pow(a[i] - b[i], 2);
  447. return (float)sqrt(sum);
  448. }
  449. float mse_array(float *a, int n)
  450. {
  451. int i;
  452. float sum = 0;
  453. for (i = 0; i < n; ++i) sum += a[i] * a[i];
  454. return (float)sqrt(sum / n);
  455. }
  456. void normalize_array(float *a, int n)
  457. {
  458. int i;
  459. float mu = mean_array(a, n);
  460. float sigma = (float)sqrt(variance_array(a, n));
  461. for (i = 0; i < n; ++i){
  462. a[i] = (a[i] - mu) / sigma;
  463. }
  464. mu = mean_array(a, n);
  465. sigma = (float)sqrt(variance_array(a, n));
  466. }
  467. void translate_array(float *a, int n, float s)
  468. {
  469. int i;
  470. for (i = 0; i < n; ++i){
  471. a[i] += s;
  472. }
  473. }
  474. float mag_array(float *a, int n)
  475. {
  476. int i;
  477. float sum = 0;
  478. for (i = 0; i < n; ++i){
  479. sum += a[i] * a[i];
  480. }
  481. return (float)sqrt(sum);
  482. }
  483. void scale_array(float *a, int n, float s)
  484. {
  485. int i;
  486. for (i = 0; i < n; ++i){
  487. a[i] *= s;
  488. }
  489. }
  490. int sample_array(float *a, int n)
  491. {
  492. float sum = sum_array(a, n);
  493. scale_array(a, n, (float)1. / sum);
  494. float r = rand_uniform(0, 1);
  495. int i;
  496. for (i = 0; i < n; ++i){
  497. r = r - a[i];
  498. if (r <= 0) return i;
  499. }
  500. return n - 1;
  501. }
  502. int max_index(float *a, int n)
  503. {
  504. if (n <= 0) return -1;
  505. int i, max_i = 0;
  506. float max = a[0];
  507. for (i = 1; i < n; ++i){
  508. if (a[i] > max){
  509. max = a[i];
  510. max_i = i;
  511. }
  512. }
  513. return max_i;
  514. }
  515. int rand_int(int min, int max)
  516. {
  517. if (max < min){
  518. int s = min;
  519. min = max;
  520. max = s;
  521. }
  522. int r = (rand() % (max - min + 1)) + min;
  523. return r;
  524. }
  525. // From http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
  526. float rand_normal()
  527. {
  528. static int haveSpare = 0;
  529. static double rand1, rand2;
  530. if (haveSpare)
  531. {
  532. haveSpare = 0;
  533. return (float)(sqrt(rand1) * sin(rand2));
  534. }
  535. haveSpare = 1;
  536. rand1 = rand() / ((double)RAND_MAX);
  537. if (rand1 < 1e-100) rand1 = 1e-100;
  538. rand1 = -2 * log(rand1);
  539. rand2 = (rand() / ((double)RAND_MAX)) * TWO_PI;
  540. return (float)(sqrt(rand1) * cos(rand2));
  541. }
  542. /*
  543. float rand_normal()
  544. {
  545. int n = 12;
  546. int i;
  547. float sum= 0;
  548. for(i = 0; i < n; ++i) sum += (float)rand()/RAND_MAX;
  549. return sum-n/2.;
  550. }
  551. */
  552. size_t rand_size_t()
  553. {
  554. return ((__int64)(rand() & 0xff) << 56) |
  555. ((__int64)(rand() & 0xff) << 48) |
  556. ((__int64)(rand() & 0xff) << 40) |
  557. ((__int64)(rand() & 0xff) << 32) |
  558. ((__int64)(rand() & 0xff) << 24) |
  559. ((__int64)(rand() & 0xff) << 16) |
  560. ((__int64)(rand() & 0xff) << 8) |
  561. ((__int64)(rand() & 0xff) << 0);
  562. }
  563. float rand_uniform(float min, float max)
  564. {
  565. if (max < min){
  566. float swap = min;
  567. min = max;
  568. max = swap;
  569. }
  570. return ((float)rand() / RAND_MAX * (max - min)) + min;
  571. }
  572. float rand_scale(float s)
  573. {
  574. float scale = rand_uniform(1, s);
  575. if (rand() % 2) return scale;
  576. return (float) 1. / scale;
  577. }
  578. float **one_hot_encode(float *a, int n, int k)
  579. {
  580. int i;
  581. float **t = (float**)calloc(n, sizeof(float*));
  582. for (i = 0; i < n; ++i){
  583. t[i] = (float*)calloc(k, sizeof(float));
  584. int index = (int)a[i];
  585. t[i][index] = 1;
  586. }
  587. return t;
  588. }