Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 1 | #!/usr/local/bin/perl |
| 2 | use Inline C; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 3 | #use Inline C => Config => BUILD_NOISY => 1, CFLAGS => $Config{cflags}." -O4 -mtune k9"; |
| 4 | use Inline C => Config => CLEAN_AFTER_BUILD => 0, ccflags => $Config{ccflags}." -O4"; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 5 | use Mojolicious::Lite; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 6 | use Mojo::JSON qw(decode_json encode_json to_json); |
Marc Kupietz | 247500f | 2015-10-09 11:29:01 +0200 | [diff] [blame] | 7 | use Encode qw(decode encode); |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 8 | use Getopt::Std; |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 9 | use Mojo::Server::Daemon; |
Marc Kupietz | d422739 | 2016-03-01 16:45:12 +0100 | [diff] [blame] | 10 | plugin 'Log::Access'; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 11 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 12 | our $opt_i = 0; # latin1-input? |
| 13 | our $opt_l = undef; |
| 14 | our $opt_p = 5676; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 15 | our $opt_m; |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 16 | our $opt_M; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 17 | our $opt_n = ''; |
| 18 | our $opt_d; |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 19 | our $opt_G; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 20 | |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 21 | my %marked; |
Marc Kupietz | 793413b | 2016-04-02 21:48:57 +0200 | [diff] [blame] | 22 | my $training_args=""; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 23 | my $mergedEnd=0; |
Marc Kupietz | 793413b | 2016-04-02 21:48:57 +0200 | [diff] [blame] | 24 | |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 25 | getopts('d:Gil:p:m:M:'); |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 26 | |
| 27 | if($opt_M) { |
Marc Kupietz | ed93021 | 2016-04-27 15:42:38 +0200 | [diff] [blame] | 28 | open my $handle, '<:encoding(UTF-8)', $opt_M |
| 29 | or die "Can't open '$opt_M' for reading: $!"; |
| 30 | while(<$handle>) { |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 31 | foreach my $mw (split /\s+/) { |
| 32 | $marked{$mw}=1 |
| 33 | } |
| 34 | } |
Marc Kupietz | ed93021 | 2016-04-27 15:42:38 +0200 | [diff] [blame] | 35 | close($handle); |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 36 | } |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 37 | |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 38 | # -cbow 1 -size 200 -window 8 -negative 25 -hs 0 -sample 1e-4 -threads 40 -binary 1 -iter 15 |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 39 | if(!$ARGV[0]) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 40 | init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0)); |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 41 | } else { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 42 | init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0)); |
Marc Kupietz | 793413b | 2016-04-02 21:48:57 +0200 | [diff] [blame] | 43 | if(open(FILE, "$ARGV[0].args")) { |
| 44 | $training_args = <FILE>; |
| 45 | } |
| 46 | close(FILE); |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 47 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 48 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 49 | if($opt_m) { |
| 50 | $mergedEnd = mergeVectors($opt_m); |
| 51 | } |
| 52 | |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 53 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 54 | if($opt_d) { # -d: dump vecs and exit |
| 55 | dump_vecs($opt_d); |
| 56 | exit; |
| 57 | } |
| 58 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 59 | my $daemon = Mojo::Server::Daemon->new( |
| 60 | app => app, |
| 61 | listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"] |
| 62 | ); |
| 63 | |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 64 | if($opt_G) { |
| 65 | print "Filtering garbage\n"; |
| 66 | filter_garbage(); |
| 67 | } |
| 68 | |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 69 | get '/' => sub { |
| 70 | my $c = shift; |
| 71 | my $word=$c->param('word'); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 72 | my $no_nbs=$c->param('n') || 100; |
| 73 | my $no_iterations=$c->param('N') || 2000; |
Marc Kupietz | d422739 | 2016-03-01 16:45:12 +0100 | [diff] [blame] | 74 | my $perplexity=$c->param('perplexity') || 20; |
Marc Kupietz | c4d62f8 | 2016-03-01 11:04:24 +0100 | [diff] [blame] | 75 | my $epsilon=$c->param('epsilon') || 5; |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 76 | my $som=$c->param('som') || 0; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 77 | my $searchBaseVocabFirst=$c->param('sbf') || 0; |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 78 | my $sort=$c->param('sort') || 0; |
Marc Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame^] | 79 | my $json=$c->param('json') || 0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 80 | my $res; |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 81 | my @lists; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 82 | my @collocations; |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 83 | if(defined($word) && $word !~ /^\s*$/) { |
| 84 | $c->inactivity_timeout(300); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 85 | $word =~ s/\s+/ /g; |
| 86 | for my $w (split(' *\| *', $word)) { |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 87 | $c->app->log->debug('Looking for neighbours of '.$w); |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 88 | if($opt_i) { |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 89 | $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst); |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 90 | } else { |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 91 | $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst); |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 92 | } |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 93 | push(@lists, $res->{paradigmatic}); |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 94 | } |
Marc Kupietz | 247500f | 2015-10-09 11:29:01 +0200 | [diff] [blame] | 95 | } |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 96 | $word =~ s/ *\| */ | /g; |
Marc Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame^] | 97 | if($json) { |
| 98 | return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}}); |
| 99 | } else { |
| 100 | $c->render(template=>"index", word=>$word, no_nbs=>$no_nbs, no_iterations => $no_iterations, epsilon=> $epsilon, perplexity=> $perplexity, show_som=>$som, searchBaseVocabFirst=>$searchBaseVocabFirst, sort=>$sort, training_args=>$training_args, mergedEnd=> $mergedEnd, marked=>\%marked, lists=> \@lists, collocators=> $res->{syntagmatic}); |
| 101 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 102 | }; |
| 103 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 104 | $daemon->run; # app->start; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 105 | |
| 106 | exit; |
| 107 | |
| 108 | __END__ |
| 109 | |
| 110 | __C__ |
| 111 | #include <stdio.h> |
| 112 | #include <string.h> |
| 113 | #include <math.h> |
| 114 | #include <malloc.h> |
| 115 | #include <stdlib.h> //strlen |
Marc Kupietz | f080976 | 2016-02-26 10:13:47 +0100 | [diff] [blame] | 116 | #include <sys/mman.h> |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 117 | #include <pthread.h> |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 118 | |
| 119 | #define max_size 2000 |
| 120 | #define max_w 50 |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 121 | #define MAX_NEIGHBOURS 1000 |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 122 | #define MAX_WORDS -1 |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 123 | #define MAX_THREADS 100 |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 124 | #define MAX_CC 50 |
| 125 | #define EXP_TABLE_SIZE 1000 |
| 126 | #define MAX_EXP 6 |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 127 | #define MIN_RESP 0.50 |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 128 | |
| 129 | //the thread function |
| 130 | void *connection_handler(void *); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 131 | |
| 132 | typedef struct { |
| 133 | long long *index; |
| 134 | float *dist; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 135 | float *norm; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 136 | long long *pos; |
Marc Kupietz | 80abb44 | 2016-03-23 21:04:08 +0100 | [diff] [blame] | 137 | int length; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 138 | } knn; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 139 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 140 | typedef struct { |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 141 | long long wordi[MAX_NEIGHBOURS]; |
| 142 | char sep[MAX_NEIGHBOURS]; |
| 143 | int length; |
| 144 | } wordlist; |
| 145 | |
| 146 | typedef struct { |
| 147 | wordlist *wl; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 148 | char *token; |
| 149 | int N; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 150 | long from; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 151 | unsigned long upto; |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 152 | float *target_sums; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 153 | } knnpars; |
| 154 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 155 | float *M, *M2=0L, *syn1neg_window, *expTable; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 156 | char *vocab; |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 157 | char *garbage = NULL; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 158 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 159 | long long words, size, merged_end; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 160 | int num_threads=20; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 161 | int latin_enc=0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 162 | int window; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 163 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 164 | int init_net(char *file_name, char *net_name, int latin) { |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 165 | FILE *f, *binvecs, *binwords; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 166 | int binwords_fd, binvecs_fd, net_fd, i; |
Marc Kupietz | 82b0267 | 2016-02-26 12:32:25 +0100 | [diff] [blame] | 167 | long long a, b, c, d, cn; |
| 168 | float len; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 169 | double val; |
Marc Kupietz | 82b0267 | 2016-02-26 12:32:25 +0100 | [diff] [blame] | 170 | |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 171 | char binvecs_fname[256], binwords_fname[256]; |
| 172 | strcpy(binwords_fname, file_name); |
| 173 | strcat(binwords_fname, ".words"); |
| 174 | strcpy(binvecs_fname, file_name); |
| 175 | strcat(binvecs_fname, ".vecs"); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 176 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 177 | latin_enc = latin; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 178 | f = fopen(file_name, "rb"); |
| 179 | if (f == NULL) { |
| 180 | printf("Input file %s not found\n", file_name); |
| 181 | return -1; |
| 182 | } |
| 183 | fscanf(f, "%lld", &words); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 184 | if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 185 | fscanf(f, "%lld", &size); |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 186 | if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) { |
| 187 | printf("Converting %s to memory mappable structures\n", file_name); |
Marc Kupietz | f080976 | 2016-02-26 10:13:47 +0100 | [diff] [blame] | 188 | vocab = (char *)malloc((long long)words * max_w * sizeof(char)); |
| 189 | M = (float *)malloc((long long)words * (long long)size * sizeof(float)); |
| 190 | if (M == NULL) { |
| 191 | printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size); |
| 192 | return -1; |
| 193 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 194 | if(strstr(file_name, ".txt")) { |
| 195 | for (b = 0; b < words; b++) { |
| 196 | a = 0; |
| 197 | while (1) { |
| 198 | vocab[b * max_w + a] = fgetc(f); |
| 199 | if (feof(f) || (vocab[b * max_w + a] == ' ')) break; |
| 200 | if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++; |
| 201 | } |
| 202 | vocab[b * max_w + a] = 0; |
| 203 | len = 0; |
| 204 | for (a = 0; a < size; a++) { |
| 205 | fscanf(f, "%lf", &val); |
| 206 | M[a + b * size] = val; |
| 207 | len += val * val; |
| 208 | } |
| 209 | len = sqrt(len); |
| 210 | for (a = 0; a < size; a++) M[a + b * size] /= len; |
| 211 | } |
| 212 | } else { |
| 213 | for (b = 0; b < words; b++) { |
| 214 | a = 0; |
| 215 | while (1) { |
| 216 | vocab[b * max_w + a] = fgetc(f); |
| 217 | if (feof(f) || (vocab[b * max_w + a] == ' ')) break; |
| 218 | if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++; |
| 219 | } |
| 220 | vocab[b * max_w + a] = 0; |
| 221 | fread(&M[b * size], sizeof(float), size, f); |
| 222 | len = 0; |
| 223 | for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size]; |
| 224 | len = sqrt(len); |
| 225 | for (a = 0; a < size; a++) M[a + b * size] /= len; |
| 226 | } |
| 227 | } |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 228 | if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) { |
| 229 | fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs); |
| 230 | fclose(binvecs); |
| 231 | fwrite(vocab, sizeof(char), (long long)words * max_w, binwords); |
| 232 | fclose(binwords); |
| 233 | } |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 234 | } |
| 235 | if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) { |
| 236 | M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0); |
| 237 | vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0); |
| 238 | if (M == MAP_FAILED || vocab == MAP_FAILED) { |
| 239 | close(binvecs_fd); |
| 240 | close(binwords_fd); |
| 241 | fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname); |
| 242 | exit(-1); |
| 243 | } |
| 244 | } else { |
| 245 | fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname); |
| 246 | exit(-1); |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 247 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 248 | fclose(f); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 249 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 250 | if(net_name && strlen(net_name) > 0) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 251 | if( (net_fd = open(net_name, O_RDONLY)) >= 0) { |
| 252 | window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2; |
| 253 | // lseek(net_fd, sizeof(float) * words * size, SEEK_SET); |
Marc Kupietz | 10bec2b | 2016-03-23 09:41:31 +0100 | [diff] [blame] | 254 | // munmap(M, sizeof(float) * words * size); |
| 255 | M2 = mmap(0, sizeof(float) * words * size + sizeof(float) * 2 * window * size * words, PROT_READ, MAP_SHARED, net_fd, 0); |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 256 | if (M2 == MAP_FAILED) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 257 | close(net_fd); |
| 258 | fprintf(stderr, "Cannot mmap %s\n", net_name); |
| 259 | exit(-1); |
| 260 | } |
Marc Kupietz | 10bec2b | 2016-03-23 09:41:31 +0100 | [diff] [blame] | 261 | syn1neg_window = M2 + words * size; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 262 | } else { |
| 263 | fprintf(stderr, "Cannot open %s\n", net_name); |
| 264 | exit(-1); |
| 265 | } |
| 266 | fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window); |
| 267 | } |
| 268 | |
| 269 | expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float)); |
| 270 | for (i = 0; i < EXP_TABLE_SIZE; i++) { |
| 271 | expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table |
| 272 | expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1) |
| 273 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 274 | return 0; |
| 275 | } |
| 276 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 277 | long mergeVectors(char *file_name){ |
| 278 | FILE *f, *binvecs, *binwords; |
| 279 | int binwords_fd, binvecs_fd, net_fd, i; |
| 280 | long long a, b, c, d, cn; |
| 281 | float len; |
| 282 | float *merge_vecs; |
| 283 | char *merge_vocab; |
| 284 | long long merge_words, merge_size; |
| 285 | |
| 286 | char binvecs_fname[256], binwords_fname[256]; |
| 287 | strcpy(binwords_fname, file_name); |
| 288 | strcat(binwords_fname, ".words"); |
| 289 | strcpy(binvecs_fname, file_name); |
| 290 | strcat(binvecs_fname, ".vecs"); |
| 291 | |
| 292 | f = fopen(file_name, "rb"); |
| 293 | if (f == NULL) { |
| 294 | printf("Input file %s not found\n", file_name); |
| 295 | exit -1; |
| 296 | } |
| 297 | fscanf(f, "%lld", &merge_words); |
| 298 | fscanf(f, "%lld", &merge_size); |
| 299 | if(merge_size != size){ |
| 300 | fprintf(stderr, "vectors must have the same length\n"); |
| 301 | exit(-1); |
| 302 | } |
| 303 | if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) { |
| 304 | merge_vecs = malloc(sizeof(float) * (words + merge_words) * size); |
| 305 | merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w); |
| 306 | if (merge_vecs == NULL || merge_vocab == NULL) { |
| 307 | close(binvecs_fd); |
| 308 | close(binwords_fd); |
| 309 | fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname); |
| 310 | exit(-1); |
| 311 | } |
| 312 | read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float)); |
| 313 | read(binwords_fd, merge_vocab, merge_words * max_w); |
| 314 | } else { |
| 315 | fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname); |
| 316 | exit(-1); |
| 317 | } |
| 318 | printf("Successfully reallocated memory\nMerging...\n"); |
| 319 | fflush(stdout); |
| 320 | memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float)); |
| 321 | memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w); |
| 322 | munmap(M, words * size * sizeof(float)); |
| 323 | munmap(vocab, words * max_w); |
| 324 | M = merge_vecs; |
| 325 | vocab = merge_vocab; |
| 326 | merged_end = merge_words; |
| 327 | words += merge_words; |
| 328 | fclose(f); |
| 329 | printf("merged_end: %lld, words: %lld\n", merged_end, words); |
| 330 | return((long) merged_end); |
| 331 | } |
| 332 | |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 333 | void filter_garbage() { |
| 334 | long i; |
Marc Kupietz | ab591a8 | 2016-04-28 14:08:49 +0200 | [diff] [blame] | 335 | unsigned char *w, previous, c; |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 336 | garbage = malloc(words); |
| 337 | memset(garbage, 0, words); |
| 338 | for (i = 0; i < words; i++) { |
| 339 | w = vocab + i * max_w; |
| 340 | previous = 0; |
Marc Kupietz | ab591a8 | 2016-04-28 14:08:49 +0200 | [diff] [blame] | 341 | while((c = *w++) && !garbage[i]) { |
| 342 | if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) || |
| 343 | (previous == '-' && (c & 32)) || |
| 344 | (previous == 0xc2 && (c == 0xa4 || c == 0xb6 )) |
| 345 | ) { |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 346 | garbage[i]=1; |
| 347 | continue; |
| 348 | } |
| 349 | previous = c; |
| 350 | } |
| 351 | } |
| 352 | return; |
| 353 | } |
| 354 | |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 355 | void *getCollocators(knnpars *pars) { |
| 356 | int N = pars->N; |
| 357 | int cc = pars->wl->wordi[0]; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 358 | knn *nbs = NULL; |
| 359 | long window_layer_size = size * window * 2; |
| 360 | long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target; |
| 361 | float f, max_f, maxmax_f; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 362 | float *target_sums, *bestf, *bestn, worstbest, wpos_sum; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 363 | long long *besti, *bestp; |
Marc Kupietz | d564258 | 2016-03-19 22:23:13 +0100 | [diff] [blame] | 364 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 365 | if(M2 == NULL || cc == -1) |
Marc Kupietz | d564258 | 2016-03-19 22:23:13 +0100 | [diff] [blame] | 366 | return NULL; |
| 367 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 368 | a = posix_memalign((void **) &target_sums, 128, words * sizeof(float)); |
| 369 | besti = malloc(N * sizeof(long long)); |
| 370 | bestp = malloc(N * sizeof(long long)); |
| 371 | bestf = malloc(N * sizeof(float)); |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 372 | bestn = malloc(N * sizeof(float)); |
| 373 | |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 374 | worstbest = MIN_RESP; |
| 375 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 376 | for (b = 0; b < words; b++) |
| 377 | target_sums[b]=0; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 378 | for (b = 0; b < N; b++) { |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 379 | besti[b] = -1; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 380 | bestn[b] = 1; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 381 | bestf[b] = worstbest; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 382 | } |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 383 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 384 | d = cc; |
| 385 | maxmax_f = -1; |
| 386 | maxmax_target = 0; |
| 387 | |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 388 | for (a = pars->from; a < pars->upto; a++) { |
| 389 | if(a >= window) |
| 390 | a++; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 391 | wpos_sum = 0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 392 | printf("window pos: %ld\n", a); |
| 393 | if (a != window) { |
| 394 | max_f = -1; |
| 395 | window_offset = a * size; |
| 396 | if (a > window) |
| 397 | window_offset -= size; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 398 | for(target = 0; target < words; target ++) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 399 | if(target == d) |
| 400 | continue; |
| 401 | f = 0; |
| 402 | for (c = 0; c < size; c++) |
Marc Kupietz | 10bec2b | 2016-03-23 09:41:31 +0100 | [diff] [blame] | 403 | f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c]; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 404 | if (f < -MAX_EXP) |
| 405 | continue; |
| 406 | else if (f > MAX_EXP) |
| 407 | continue; |
| 408 | else |
| 409 | f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))]; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 410 | wpos_sum += f; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 411 | |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 412 | target_sums[target] += f; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 413 | if(f > worstbest) { |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 414 | for (b = 0; b < N; b++) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 415 | if (f > bestf[b]) { |
Marc Kupietz | 33679a3 | 2016-03-22 08:49:39 +0100 | [diff] [blame] | 416 | memmove(bestf + b + 1, bestf + b, (N - b -1) * sizeof(float)); |
| 417 | memmove(besti + b + 1, besti + b, (N - b -1) * sizeof(long long)); |
| 418 | memmove(bestp + b + 1, bestp + b, (N - b -1) * sizeof(long long)); |
| 419 | bestf[b] = f; |
| 420 | besti[b] = target; |
| 421 | bestp[b] = window-a; |
| 422 | break; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 423 | } |
| 424 | } |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 425 | if(b == N - 1) |
| 426 | worstbest = bestf[N-1]; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | printf("%d %.2f\n", max_target, max_f); |
| 430 | printf("%s (%.2f) ", &vocab[max_target * max_w], max_f); |
| 431 | if(max_f > maxmax_f) { |
| 432 | maxmax_f = max_f; |
| 433 | maxmax_target = max_target; |
| 434 | } |
Marc Kupietz | 33679a3 | 2016-03-22 08:49:39 +0100 | [diff] [blame] | 435 | for (b = 0; b < N; b++) |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 436 | if(bestp[b] == window-a) |
| 437 | bestn[b] = bestf[b] / wpos_sum; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 438 | } else { |
| 439 | printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]); |
| 440 | } |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 441 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 442 | } |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 443 | for (b = 0; b < words; b++) |
| 444 | pars->target_sums[b] += (target_sums[b] / wpos_sum ) / (window * 2); |
| 445 | free(target_sums); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 446 | for(b=0; b<N && besti[b] >= 0; b++) // THIS LOOP IS NEEDED (b...) |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 447 | printf("%s %.2f %d * ", &vocab[besti[b]*max_w], bestf[b], bestp[b]); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 448 | printf("\n"); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 449 | nbs = malloc(sizeof(knn)); |
| 450 | nbs->index = besti; |
| 451 | nbs->dist = bestf; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 452 | nbs->norm = bestn; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 453 | nbs->pos = bestp; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 454 | nbs->length = b-1; |
| 455 | pthread_exit(nbs); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 456 | } |
| 457 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 458 | wordlist *getTargetWords(char *st1, int search_backw) { |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 459 | wordlist *wl = malloc(sizeof(wordlist)); |
| 460 | char st[100][max_size], sep[100]; |
| 461 | long a, b=0, c=0, cn=0; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 462 | int unmerged; |
| 463 | |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 464 | while (1) { |
| 465 | st[cn][b] = st1[c]; |
| 466 | b++; |
| 467 | c++; |
| 468 | st[cn][b] = 0; |
| 469 | if (st1[c] == 0) break; |
Marc Kupietz | 95aa1c0 | 2016-03-15 09:40:43 +0100 | [diff] [blame] | 470 | if (st1[c] == ' ' || st1[c] == '-') { |
| 471 | sep[cn++] = st1[c]; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 472 | b = 0; |
| 473 | c++; |
| 474 | } |
| 475 | } |
| 476 | cn++; |
| 477 | for (a = 0; a < cn; a++) { |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 478 | if(search_backw) { |
| 479 | for (b = words - 1; b >= 0; b--) if (!strcmp(&vocab[b * max_w], st[a])) break; |
| 480 | } else { |
| 481 | for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break; |
| 482 | } |
Marc Kupietz | 34a3ee9 | 2016-02-27 22:43:16 +0100 | [diff] [blame] | 483 | if (b == words) b = -1; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 484 | wl->wordi[a] = b; |
| 485 | fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], wl->wordi[a]); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 486 | if (b == -1) { |
Marc Kupietz | e8da306 | 2016-02-25 08:37:53 +0100 | [diff] [blame] | 487 | fprintf(stderr, "Out of dictionary word!\n"); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 488 | cn--; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 489 | break; |
| 490 | } |
| 491 | } |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 492 | wl->length=cn; |
| 493 | return(wl); |
| 494 | } |
| 495 | |
| 496 | void *_get_neighbours(knnpars *pars) { |
| 497 | char *st1 = pars->token; |
| 498 | int N = pars->N; |
| 499 | long from = pars -> from; |
| 500 | unsigned long upto = pars -> upto; |
| 501 | char file_name[max_size], st[100][max_size], *sep; |
| 502 | float dist, len, *bestd, vec[max_size]; |
| 503 | long long a, b, c, d, cn, *bi, *besti; |
| 504 | char ch; |
| 505 | knn *nbs = NULL; |
| 506 | wordlist *wl = pars->wl; |
| 507 | |
| 508 | besti = malloc(N * sizeof(long long)); |
| 509 | bestd = malloc(N * sizeof(float)); |
| 510 | |
| 511 | float worstbest=-1; |
| 512 | |
| 513 | for (a = 0; a < N; a++) bestd[a] = 0; |
| 514 | a = 0; |
| 515 | bi = wl->wordi; |
| 516 | cn = wl->length; |
| 517 | sep = wl->sep; |
| 518 | b = bi[0]; |
| 519 | c = 0; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 520 | if (b == -1) { |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 521 | N = 0; |
| 522 | goto end; |
| 523 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 524 | for (a = 0; a < size; a++) vec[a] = 0; |
| 525 | for (b = 0; b < cn; b++) { |
| 526 | if (bi[b] == -1) continue; |
Marc Kupietz | 95aa1c0 | 2016-03-15 09:40:43 +0100 | [diff] [blame] | 527 | if(b>0 && sep[b-1] == '-') |
| 528 | for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size]; |
| 529 | else |
| 530 | for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size]; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 531 | } |
| 532 | len = 0; |
| 533 | for (a = 0; a < size; a++) len += vec[a] * vec[a]; |
| 534 | len = sqrt(len); |
| 535 | for (a = 0; a < size; a++) vec[a] /= len; |
| 536 | for (a = 0; a < N; a++) bestd[a] = -1; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 537 | for (c = from; c < upto; c++) { |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 538 | if(garbage && garbage[c]) continue; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 539 | a = 0; |
Marc Kupietz | 34020dc | 2016-02-25 08:44:19 +0100 | [diff] [blame] | 540 | // do not skip taget word |
Marc Kupietz | e8da306 | 2016-02-25 08:37:53 +0100 | [diff] [blame] | 541 | // for (b = 0; b < cn; b++) if (bi[b] == c) a = 1; |
| 542 | // if (a == 1) continue; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 543 | dist = 0; |
| 544 | for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size]; |
Marc Kupietz | be1b9fc | 2016-02-26 10:34:30 +0100 | [diff] [blame] | 545 | if(dist > worstbest) { |
| 546 | for (a = 0; a < N; a++) { |
| 547 | if (dist > bestd[a]) { |
Marc Kupietz | 33679a3 | 2016-03-22 08:49:39 +0100 | [diff] [blame] | 548 | memmove(bestd + a + 1, bestd + a, (N - a -1) * sizeof(float)); |
| 549 | memmove(besti + a + 1, besti + a, (N - a -1) * sizeof(long long)); |
Marc Kupietz | be1b9fc | 2016-02-26 10:34:30 +0100 | [diff] [blame] | 550 | bestd[a] = dist; |
| 551 | besti[a] = c; |
| 552 | break; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 553 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 554 | } |
Marc Kupietz | be1b9fc | 2016-02-26 10:34:30 +0100 | [diff] [blame] | 555 | worstbest = bestd[N-1]; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 556 | } |
| 557 | } |
Marc Kupietz | 34020dc | 2016-02-25 08:44:19 +0100 | [diff] [blame] | 558 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 559 | nbs = malloc(sizeof(knn)); |
| 560 | nbs->index = besti; |
| 561 | nbs->dist = bestd; |
| 562 | nbs->length = N; |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 563 | end: |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 564 | pthread_exit(nbs); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 565 | } |
| 566 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 567 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 568 | SV *get_neighbours(char *st1, int N, int sort_by, int search_backw) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 569 | HV *result = newHV(); |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 570 | float *target_sums, bestd[MAX_NEIGHBOURS], bestn[MAX_NEIGHBOURS], bests[MAX_NEIGHBOURS], vec[max_size]; |
Marc Kupietz | 50485ba | 2016-03-23 09:13:14 +0100 | [diff] [blame] | 571 | long besti[MAX_NEIGHBOURS], bestp[MAX_NEIGHBOURS], a, b, c, d, slice; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 572 | knn *para_nbs[MAX_THREADS]; |
| 573 | knn *syn_nbs[MAX_THREADS]; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 574 | knnpars pars[MAX_THREADS]; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 575 | pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t)); |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 576 | wordlist *wl; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 577 | int syn_threads = (M2? window * 2 : 0); |
| 578 | int para_threads = num_threads - syn_threads; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 579 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 580 | if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS; |
| 581 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 582 | slice = words / para_threads; |
| 583 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 584 | wl = getTargetWords(st1, search_backw); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 585 | if(wl->length < 1) |
| 586 | goto end; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 587 | |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 588 | a = posix_memalign((void **) &target_sums, 128, words * sizeof(float)); |
| 589 | for(a = 0; a < words; a++) |
| 590 | target_sums[a] = 0; |
| 591 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 592 | printf("Starting %d threads\n", para_threads); |
| 593 | fflush(stdout); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 594 | for(a=0; a < para_threads; a++) { |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 595 | pars[a].token = st1; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 596 | pars[a].wl = wl; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 597 | pars[a].N = N; |
| 598 | pars[a].from = a*slice; |
| 599 | pars[a].upto = ((a+1)*slice > words? words:(a+1)*slice); |
| 600 | pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]); |
| 601 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 602 | if(M2) { |
| 603 | for(a=0; a < syn_threads; a++) { |
| 604 | pars[a + para_threads].target_sums = target_sums; |
| 605 | pars[a + para_threads].wl = wl; |
| 606 | pars[a + para_threads].N = N; |
| 607 | pars[a + para_threads].from = a; |
| 608 | pars[a + para_threads].upto = a+1; |
| 609 | pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]); |
| 610 | } |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 611 | } |
| 612 | printf("Waiting for para threads to join\n"); |
| 613 | fflush(stdout); |
| 614 | for (a = 0; a < para_threads; a++) pthread_join(pt[a], ¶_nbs[a]); |
| 615 | printf("Para threads joint\n"); |
| 616 | fflush(stdout); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 617 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 618 | /* if(!syn_nbs[0]) */ |
| 619 | /* goto end; */ |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 620 | |
| 621 | for(b=0; b < N; b++) { |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 622 | besti[b] = para_nbs[0]->index[b]; |
| 623 | bestd[b] = para_nbs[0]->dist[b]; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 624 | } |
| 625 | |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 626 | for(a=1; a < para_threads; a++) { |
| 627 | for(b=0; b < para_nbs[a]->length && para_nbs[a]->index[b] >= 0; b++) { |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 628 | for(c=0; c < N; c++) { |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 629 | if(para_nbs[a]->dist[b] > bestd[c]) { |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 630 | for(d=N-1; d>c; d--) { |
| 631 | bestd[d] = bestd[d-1]; |
| 632 | besti[d] = besti[d-1]; |
| 633 | } |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 634 | besti[c] = para_nbs[a]->index[b]; |
| 635 | bestd[c] = para_nbs[a]->dist[b]; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 636 | break; |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 642 | AV* array = newAV(); |
| 643 | for (a = 0; a < N; a++) { |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 644 | HV* hash = newHV(); |
Marc Kupietz | 50485ba | 2016-03-23 09:13:14 +0100 | [diff] [blame] | 645 | SV* word = newSVpvf(&vocab[besti[a] * max_w], 0); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 646 | if(latin_enc == 0) SvUTF8_on(word); |
| 647 | hv_store(hash, "word", strlen("word"), word , 0); |
| 648 | hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0); |
| 649 | hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0); |
| 650 | AV *vector = newAV(); |
| 651 | for (b = 0; b < size; b++) { |
| 652 | av_push(vector, newSVnv(M[b + besti[a] * size])); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 653 | } |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 654 | hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0); |
| 655 | av_push(array, newRV_noinc((SV*)hash)); |
| 656 | } |
| 657 | hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0); |
| 658 | |
Marc Kupietz | 50485ba | 2016-03-23 09:13:14 +0100 | [diff] [blame] | 659 | for(b=0; b < MAX_NEIGHBOURS; b++) { |
| 660 | besti[b] = -1L; |
| 661 | bestd[b] = 0; |
| 662 | bestn[b] = 0; |
| 663 | bestp[b] = 0; |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 664 | bests[b] = 0; |
Marc Kupietz | 50485ba | 2016-03-23 09:13:14 +0100 | [diff] [blame] | 665 | } |
| 666 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 667 | if (M2) { |
| 668 | printf("Waiting for syn threads to join\n"); |
| 669 | fflush(stdout); |
| 670 | for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], &syn_nbs[a]); |
| 671 | printf("syn threads joint\n"); |
| 672 | fflush(stdout); |
Marc Kupietz | 50485ba | 2016-03-23 09:13:14 +0100 | [diff] [blame] | 673 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 674 | for(b=0; b < syn_nbs[0]->length; b++) { |
| 675 | besti[b] = syn_nbs[0]->index[b]; |
| 676 | bestd[b] = syn_nbs[0]->dist[b]; |
| 677 | bestn[b] = syn_nbs[0]->norm[b]; |
| 678 | bestp[b] = syn_nbs[0]->pos[b]; |
| 679 | bests[b] = target_sums[syn_nbs[0]->index[b]]; |
| 680 | } |
| 681 | |
| 682 | if(sort_by != 1) { // sort by responsiveness |
| 683 | for(a=1; a < syn_threads; a++) { |
| 684 | for(b=0; b < syn_nbs[a]->length; b++) { |
| 685 | for(c=0; c < MAX_NEIGHBOURS; c++) { |
| 686 | if(syn_nbs[a]->dist[b] > bestd[c]) { |
| 687 | for(d=MAX_NEIGHBOURS-1; d>c; d--) { |
| 688 | bestd[d] = bestd[d-1]; |
| 689 | besti[d] = besti[d-1]; |
| 690 | bestn[d] = bestn[d-1]; |
| 691 | bestp[d] = bestp[d-1]; |
| 692 | } |
| 693 | besti[c] = syn_nbs[a]->index[b]; |
| 694 | bestd[c] = syn_nbs[a]->dist[b]; |
| 695 | bestn[c] = syn_nbs[a]->norm[b]; |
| 696 | bestp[c] = syn_nbs[a]->pos[b]; |
| 697 | break; |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 698 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | } |
| 702 | } else { // sort by mean p |
| 703 | for(a=1; a < syn_threads; a++) { |
| 704 | for(b=0; b < syn_nbs[a]->length; b++) { |
| 705 | for(c=0; c < MAX_NEIGHBOURS; c++) { |
| 706 | if(target_sums[syn_nbs[a]->index[b]] > bests[c]) { |
| 707 | for(d=MAX_NEIGHBOURS-1; d>c; d--) { |
| 708 | bestd[d] = bestd[d-1]; |
| 709 | besti[d] = besti[d-1]; |
| 710 | bestn[d] = bestn[d-1]; |
| 711 | bestp[d] = bestp[d-1]; |
| 712 | bests[d] = bests[d-1]; |
| 713 | } |
| 714 | besti[c] = syn_nbs[a]->index[b]; |
| 715 | bestd[c] = syn_nbs[a]->dist[b]; |
| 716 | bestn[c] = syn_nbs[a]->norm[b]; |
| 717 | bestp[c] = syn_nbs[a]->pos[b]; |
| 718 | bests[c] = target_sums[syn_nbs[a]->index[b]]; |
| 719 | break; |
| 720 | } |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 721 | } |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 725 | array = newAV(); |
| 726 | for (a = 0; a < MAX_NEIGHBOURS && besti[a] >= 0; a++) { |
| 727 | HV* hash = newHV(); |
| 728 | SV* word = newSVpvf(&vocab[besti[a] * max_w], 0); |
| 729 | if(latin_enc == 0) SvUTF8_on(word); |
| 730 | hv_store(hash, "word", strlen("word"), word , 0); |
| 731 | hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0); |
| 732 | hv_store(hash, "norm", strlen("norm"), newSVnv(bestn[a]), 0); |
| 733 | hv_store(hash, "sum", strlen("sum"), newSVnv(target_sums[besti[a]]), 0); |
| 734 | hv_store(hash, "pos", strlen("pos"), newSVnv(bestp[a]), 0); |
| 735 | av_push(array, newRV_noinc((SV*)hash)); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 736 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 737 | hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 738 | } |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 739 | end: |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 740 | return newRV_noinc((SV*)result); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 741 | } |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 742 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 743 | int dump_vecs(char *fname) { |
| 744 | long i, j; |
| 745 | FILE *f; |
| 746 | /* if(words>200000) */ |
| 747 | /* words=200000; */ |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 748 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 749 | if((f=fopen(fname, "w")) == NULL) { |
| 750 | fprintf(stderr, "cannot open %s for writing\n", fname); |
| 751 | return(-1); |
| 752 | } |
| 753 | fprintf(f, "%lld %lld\n", words, size); |
| 754 | for (i=0; i < words; i++) { |
| 755 | fprintf(f, "%s ", &vocab[i * max_w]); |
| 756 | for(j=0; j < size - 1; j++) |
| 757 | fprintf(f, "%f ", M[i*size + j]); |
| 758 | fprintf(f, "%f\n", M[i*size + j]); |
| 759 | } |
| 760 | fclose(f); |
| 761 | return(0); |
| 762 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 763 | |