w2v-server.pl: use mmap instead of fread
diff --git a/w2v-server.pl b/w2v-server.pl
index f4f83f6..cc08e4b 100644
--- a/w2v-server.pl
+++ b/w2v-server.pl
@@ -38,6 +38,7 @@
#include <math.h>
#include <malloc.h>
#include <stdlib.h> //strlen
+#include <sys/mman.h>
#define max_size 2000
#define max_w 50
@@ -58,6 +59,7 @@
int init_net(char *file_name) {
FILE *f, *binvecs, *binwords;
+ int binwords_fd, binvecs_fd;
char binvecs_fname[256], binwords_fname[256];
strcpy(binwords_fname, file_name);
strcat(binwords_fname, ".words");
@@ -73,19 +75,23 @@
fscanf(f, "%lld", &words);
if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
fscanf(f, "%lld", &size);
- vocab = (char *)malloc((long long)words * max_w * sizeof(char));
for (a = 0; a < MAX_NEIGHBOURS; a++) bestw[a] = (char *)malloc(max_size * sizeof(char));
- M = (float *)malloc((long long)words * (long long)size * sizeof(float));
- if (M == NULL) {
- printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
- return -1;
- }
- if( (binvecs = fopen(binvecs_fname, "rb")) != NULL && (binwords = fopen(binwords_fname, "rb")) != NULL) {
- fread(M, sizeof(float), (long long)words * (long long)size, binvecs);
- fclose(binvecs);
- fread(vocab, sizeof(char), (long long)words * max_w, binwords);
- fclose(binwords);
+ if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
+ M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_PRIVATE, binvecs_fd, 0);
+ vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_PRIVATE, binwords_fd, 0);
+ if (M == MAP_FAILED || vocab == MAP_FAILED) {
+ close(binvecs_fd);
+ close(binwords_fd);
+ fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
+ exit(-1);
+ }
} else {
+ vocab = (char *)malloc((long long)words * max_w * sizeof(char));
+ M = (float *)malloc((long long)words * (long long)size * sizeof(float));
+ if (M == NULL) {
+ printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
+ return -1;
+ }
for (b = 0; b < words; b++) {
a = 0;
while (1) {