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.

669 lines
12 KiB

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