Mercurial > repos > bitlab > imsame
comparison IMSAME/src/commonFunctions.c @ 0:762009a91895 draft
Uploaded
author | bitlab |
---|---|
date | Sat, 15 Dec 2018 18:04:10 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:762009a91895 |
---|---|
1 #include <stdio.h> | |
2 #include <stdlib.h> | |
3 #include <sys/time.h> | |
4 #include <inttypes.h> | |
5 #include <ctype.h> | |
6 #include <string.h> | |
7 #include <pthread.h> | |
8 #include <emmintrin.h> | |
9 #include <math.h> | |
10 #include "structs.h" | |
11 | |
12 #define ALIGNED_BYTES 16 | |
13 | |
14 void terror(char *s) { | |
15 fprintf(stdout, "ERR**** %s ****\n", s); | |
16 exit(-1); | |
17 } | |
18 | |
19 char buffered_fgetc(char *buffer, uint64_t *pos, uint64_t *read, FILE *f) { | |
20 if (*pos >= READBUF) { | |
21 *pos = 0; | |
22 memset(buffer, 0, READBUF); | |
23 *read = fread(buffer, 1, READBUF, f); | |
24 } | |
25 *pos = *pos + 1; | |
26 return buffer[*pos-1]; | |
27 } | |
28 | |
29 void get_num_seqs_and_length(char * seq_buffer, uint64_t * n_seqs, uint64_t * t_len, LoadingDBArgs * ldbargs){ | |
30 char check[16] = ">>>>>>>>>>>>>>>>"; | |
31 uint64_t a_fourth = *t_len / 4; | |
32 | |
33 uint64_t i=0; | |
34 | |
35 __m128i * ptr1; | |
36 __m128i * ptr2; | |
37 __m128i res = _mm_setzero_si128(); | |
38 ptr2 = (__m128i *) check; | |
39 | |
40 ldbargs[0].read_from = 0; | |
41 for(i=0; i<a_fourth - 16; i+=16){ | |
42 ptr1 = (__m128i *) &seq_buffer[i]; | |
43 res = _mm_cmpeq_epi8(*ptr1, *ptr2); | |
44 // TODO parallelize this | |
45 *n_seqs += (uint64_t) (((unsigned char *) &res)[0]) >> 7; | |
46 *n_seqs += (uint64_t) (((unsigned char *) &res)[1]) >> 7; | |
47 *n_seqs += (uint64_t) (((unsigned char *) &res)[2]) >> 7; | |
48 *n_seqs += (uint64_t) (((unsigned char *) &res)[3]) >> 7; | |
49 *n_seqs += (uint64_t) (((unsigned char *) &res)[4]) >> 7; | |
50 *n_seqs += (uint64_t) (((unsigned char *) &res)[5]) >> 7; | |
51 *n_seqs += (uint64_t) (((unsigned char *) &res)[6]) >> 7; | |
52 *n_seqs += (uint64_t) (((unsigned char *) &res)[7]) >> 7; | |
53 *n_seqs += (uint64_t) (((unsigned char *) &res)[8]) >> 7; | |
54 *n_seqs += (uint64_t) (((unsigned char *) &res)[9]) >> 7; | |
55 *n_seqs += (uint64_t) (((unsigned char *) &res)[10]) >> 7; | |
56 *n_seqs += (uint64_t) (((unsigned char *) &res)[11]) >> 7; | |
57 *n_seqs += (uint64_t) (((unsigned char *) &res)[12]) >> 7; | |
58 *n_seqs += (uint64_t) (((unsigned char *) &res)[13]) >> 7; | |
59 *n_seqs += (uint64_t) (((unsigned char *) &res)[14]) >> 7; | |
60 *n_seqs += (uint64_t) (((unsigned char *) &res)[15]) >> 7; | |
61 } | |
62 while(seq_buffer[i] != '>'){ ++i; } | |
63 ldbargs[0].read_to = i; | |
64 ldbargs[0].data_database->n_seqs = *n_seqs; | |
65 ldbargs[1].read_from = i; | |
66 while(i % 16 != 0){ ++i; if(seq_buffer[i] == '>') terror("Sequence of length < 16"); } | |
67 if(i > ldbargs[1].read_from) ++(*n_seqs); // For the advance to align bytes | |
68 | |
69 //printf("seqs at 0: %"PRIu64" readfrom: %"PRIu64", readto: %"PRIu64", \n", ldbargs[0].data_database->n_seqs, ldbargs[0].read_from, ldbargs[0].read_to); | |
70 | |
71 | |
72 for(i=i; i<2*(a_fourth) - 16; i+=16){ | |
73 ptr1 = (__m128i *) &seq_buffer[i]; | |
74 res = _mm_cmpeq_epi8(*ptr1, *ptr2); | |
75 // TODO parallelize this | |
76 *n_seqs += (uint64_t) (((unsigned char *) &res)[0]) >> 7; | |
77 *n_seqs += (uint64_t) (((unsigned char *) &res)[1]) >> 7; | |
78 *n_seqs += (uint64_t) (((unsigned char *) &res)[2]) >> 7; | |
79 *n_seqs += (uint64_t) (((unsigned char *) &res)[3]) >> 7; | |
80 *n_seqs += (uint64_t) (((unsigned char *) &res)[4]) >> 7; | |
81 *n_seqs += (uint64_t) (((unsigned char *) &res)[5]) >> 7; | |
82 *n_seqs += (uint64_t) (((unsigned char *) &res)[6]) >> 7; | |
83 *n_seqs += (uint64_t) (((unsigned char *) &res)[7]) >> 7; | |
84 *n_seqs += (uint64_t) (((unsigned char *) &res)[8]) >> 7; | |
85 *n_seqs += (uint64_t) (((unsigned char *) &res)[9]) >> 7; | |
86 *n_seqs += (uint64_t) (((unsigned char *) &res)[10]) >> 7; | |
87 *n_seqs += (uint64_t) (((unsigned char *) &res)[11]) >> 7; | |
88 *n_seqs += (uint64_t) (((unsigned char *) &res)[12]) >> 7; | |
89 *n_seqs += (uint64_t) (((unsigned char *) &res)[13]) >> 7; | |
90 *n_seqs += (uint64_t) (((unsigned char *) &res)[14]) >> 7; | |
91 *n_seqs += (uint64_t) (((unsigned char *) &res)[15]) >> 7; | |
92 } | |
93 while(seq_buffer[i] != '>') ++i; | |
94 ldbargs[1].read_to = i; | |
95 ldbargs[1].data_database->n_seqs = *n_seqs - ldbargs[0].data_database->n_seqs; | |
96 ldbargs[2].read_from = i; | |
97 while(i % 16 != 0){ ++i; if(seq_buffer[i] == '>') terror("Sequence of length < 16"); } | |
98 if(i > ldbargs[2].read_from) ++(*n_seqs); // For the advance to align bytes | |
99 | |
100 //printf("seqs at 1: %"PRIu64" readfrom: %"PRIu64", readto: %"PRIu64", \n", ldbargs[1].data_database->n_seqs, ldbargs[1].read_from, ldbargs[1].read_to); | |
101 | |
102 | |
103 for(i=i; i<3*(a_fourth) - 16; i+=16){ | |
104 ptr1 = (__m128i *) &seq_buffer[i]; | |
105 res = _mm_cmpeq_epi8(*ptr1, *ptr2); | |
106 // TODO parallelize this | |
107 *n_seqs += (uint64_t) (((unsigned char *) &res)[0]) >> 7; | |
108 *n_seqs += (uint64_t) (((unsigned char *) &res)[1]) >> 7; | |
109 *n_seqs += (uint64_t) (((unsigned char *) &res)[2]) >> 7; | |
110 *n_seqs += (uint64_t) (((unsigned char *) &res)[3]) >> 7; | |
111 *n_seqs += (uint64_t) (((unsigned char *) &res)[4]) >> 7; | |
112 *n_seqs += (uint64_t) (((unsigned char *) &res)[5]) >> 7; | |
113 *n_seqs += (uint64_t) (((unsigned char *) &res)[6]) >> 7; | |
114 *n_seqs += (uint64_t) (((unsigned char *) &res)[7]) >> 7; | |
115 *n_seqs += (uint64_t) (((unsigned char *) &res)[8]) >> 7; | |
116 *n_seqs += (uint64_t) (((unsigned char *) &res)[9]) >> 7; | |
117 *n_seqs += (uint64_t) (((unsigned char *) &res)[10]) >> 7; | |
118 *n_seqs += (uint64_t) (((unsigned char *) &res)[11]) >> 7; | |
119 *n_seqs += (uint64_t) (((unsigned char *) &res)[12]) >> 7; | |
120 *n_seqs += (uint64_t) (((unsigned char *) &res)[13]) >> 7; | |
121 *n_seqs += (uint64_t) (((unsigned char *) &res)[14]) >> 7; | |
122 *n_seqs += (uint64_t) (((unsigned char *) &res)[15]) >> 7; | |
123 } | |
124 while(seq_buffer[i] != '>') ++i; | |
125 ldbargs[2].read_to = i; | |
126 ldbargs[2].data_database->n_seqs = *n_seqs - (ldbargs[1].data_database->n_seqs + ldbargs[0].data_database->n_seqs); | |
127 ldbargs[3].read_from = i; | |
128 while(i % 16 != 0){ ++i; if(seq_buffer[i] == '>') terror("Sequence of length < 16"); } | |
129 if(i > ldbargs[3].read_from) ++(*n_seqs); // For the advance to align bytes | |
130 | |
131 //printf("seqs at 2: %"PRIu64" readfrom: %"PRIu64", readto: %"PRIu64", \n", ldbargs[2].data_database->n_seqs, ldbargs[2].read_from, ldbargs[2].read_to); | |
132 | |
133 | |
134 for(i=i; i<(*t_len) - 16; i+=16){ | |
135 ptr1 = (__m128i *) &seq_buffer[i]; | |
136 res = _mm_cmpeq_epi8(*ptr1, *ptr2); | |
137 // TODO parallelize this | |
138 *n_seqs += (uint64_t) (((unsigned char *) &res)[0]) >> 7; | |
139 *n_seqs += (uint64_t) (((unsigned char *) &res)[1]) >> 7; | |
140 *n_seqs += (uint64_t) (((unsigned char *) &res)[2]) >> 7; | |
141 *n_seqs += (uint64_t) (((unsigned char *) &res)[3]) >> 7; | |
142 *n_seqs += (uint64_t) (((unsigned char *) &res)[4]) >> 7; | |
143 *n_seqs += (uint64_t) (((unsigned char *) &res)[5]) >> 7; | |
144 *n_seqs += (uint64_t) (((unsigned char *) &res)[6]) >> 7; | |
145 *n_seqs += (uint64_t) (((unsigned char *) &res)[7]) >> 7; | |
146 *n_seqs += (uint64_t) (((unsigned char *) &res)[8]) >> 7; | |
147 *n_seqs += (uint64_t) (((unsigned char *) &res)[9]) >> 7; | |
148 *n_seqs += (uint64_t) (((unsigned char *) &res)[10]) >> 7; | |
149 *n_seqs += (uint64_t) (((unsigned char *) &res)[11]) >> 7; | |
150 *n_seqs += (uint64_t) (((unsigned char *) &res)[12]) >> 7; | |
151 *n_seqs += (uint64_t) (((unsigned char *) &res)[13]) >> 7; | |
152 *n_seqs += (uint64_t) (((unsigned char *) &res)[14]) >> 7; | |
153 *n_seqs += (uint64_t) (((unsigned char *) &res)[15]) >> 7; | |
154 } | |
155 while(i < *t_len){ | |
156 if(seq_buffer[i] == '>') ++(*n_seqs); | |
157 ++i; | |
158 } | |
159 ldbargs[3].read_to = *t_len; | |
160 ldbargs[3].data_database->n_seqs = *n_seqs - (ldbargs[2].data_database->n_seqs + ldbargs[1].data_database->n_seqs + ldbargs[0].data_database->n_seqs); | |
161 | |
162 //printf("seqs at 3: %"PRIu64" readfrom: %"PRIu64", readto: %"PRIu64", \n", ldbargs[3].data_database->n_seqs, ldbargs[3].read_from, ldbargs[3].read_to); | |
163 | |
164 } | |
165 | |
166 Queue * generate_queue(Head * queue_head, uint64_t t_reads, uint64_t n_threads, uint64_t levels){ | |
167 uint64_t i, j; | |
168 uint64_t reads_per_thread; | |
169 if(levels > t_reads) levels = t_reads; | |
170 uint64_t pieces = t_reads/levels; | |
171 uint64_t from, to, t_queues = 0, current_queue = 0; | |
172 for(i=0;i<levels;i++) t_queues += ((i+1)*n_threads); | |
173 if(t_queues > t_reads) t_queues = t_reads; | |
174 Queue * queues = (Queue *) malloc(t_queues * sizeof(Queue)); | |
175 if(queues == NULL) terror("Could not allocate queue tasks"); | |
176 queue_head->head = &queues[0]; | |
177 | |
178 | |
179 for(i=0;i<levels;i++){ | |
180 | |
181 //reads_per_thread = (uint64_t) (floorl((long double) pieces / (long double) ((i+1)*n_threads))); | |
182 reads_per_thread = (uint64_t) (ceill((long double) pieces / (long double) ((i+1)*n_threads))); | |
183 | |
184 | |
185 for(j=0;j<(i+1)*n_threads;j++){ | |
186 from = j * reads_per_thread + (pieces*i); | |
187 to = (j + 1) * reads_per_thread + (pieces*i); | |
188 | |
189 if(j == (i+1)*n_threads - 1) to = pieces*(i+1); | |
190 | |
191 if(from >= t_reads){ | |
192 queues[current_queue-1].next = NULL; | |
193 return &queues[0]; | |
194 } | |
195 | |
196 | |
197 if(i == levels - 1 && j == (i+1)*n_threads - 1){ | |
198 //If its the last | |
199 queues[current_queue].next = NULL; | |
200 }else{ | |
201 //Else add it to the queue | |
202 queues[current_queue].next = &queues[current_queue+1]; | |
203 } | |
204 | |
205 | |
206 queues[current_queue].r1 = from; | |
207 queues[current_queue].r2 = (to <= t_reads) ? (to) : (t_reads); | |
208 current_queue++; | |
209 //printf("current_piece: %"PRIu64"-%"PRIu64" diff: %"PRIu64"\n", from, to, to - from); | |
210 | |
211 } | |
212 | |
213 } | |
214 //printf("TREADS was %"PRIu64"\n", t_reads); | |
215 queues[current_queue-1].r2 = t_reads; | |
216 return &queues[0]; | |
217 } | |
218 | |
219 void print_queue(Queue * q){ | |
220 fprintf(stdout, "Task: %"PRIu64"-%"PRIu64"\n", q->r1, q->r2); | |
221 } | |
222 | |
223 Queue * get_task_from_queue(Head * queue_head, pthread_mutex_t * lock){ | |
224 pthread_mutex_lock(lock); | |
225 | |
226 Queue * ptr = queue_head->head; | |
227 if(queue_head->head != NULL) queue_head->head = queue_head->head->next; | |
228 //if(ptr != NULL){ printf("Taking "); /*print_queue(ptr);*/ } | |
229 | |
230 pthread_mutex_unlock(lock); | |
231 | |
232 | |
233 return ptr; | |
234 } | |
235 | |
236 uint64_t quick_pow4(uint64_t n){ | |
237 static uint64_t pow4[33]={1L, 4L, 16L, 64L, 256L, 1024L, 4096L, 16384L, 65536L, | |
238 262144L, 1048576L, 4194304L, 16777216L, 67108864L, 268435456L, 1073741824L, 4294967296L, | |
239 17179869184L, 68719476736L, 274877906944L, 1099511627776L, 4398046511104L, 17592186044416L, | |
240 70368744177664L, 281474976710656L, 1125899906842624L, 4503599627370496L, 18014398509481984L, | |
241 72057594037927936L, 288230376151711744L, 1152921504606846976L, 4611686018427387904L}; | |
242 return pow4[n]; | |
243 } | |
244 | |
245 uint64_t quick_pow4byLetter(uint64_t n, const char c){ | |
246 static uint64_t pow4_G[33]={2*1L, 2*4L, 2*16L, 2*64L, 2*256L, 2*1024L, 2*4096L, 2*16384L, 2*65536L, | |
247 (uint64_t)2*262144L, (uint64_t)2*1048576L,(uint64_t)2*4194304L, (uint64_t)2*16777216L, (uint64_t)2*67108864L, (uint64_t)2*268435456L, (uint64_t)2*1073741824L, (uint64_t)2*4294967296L, | |
248 (uint64_t)2*17179869184L, (uint64_t)2*68719476736L, (uint64_t)2*274877906944L, (uint64_t)2*1099511627776L, (uint64_t)2*4398046511104L, (uint64_t)2*17592186044416L, | |
249 (uint64_t)2*70368744177664L, (uint64_t)2*281474976710656L, (uint64_t)2*1125899906842624L, (uint64_t)2*4503599627370496L, (uint64_t)2*18014398509481984L, | |
250 (uint64_t)2*72057594037927936L, (uint64_t) 2*288230376151711744L, (uint64_t) 2*1152921504606846976L, (uint64_t) 2*4611686018427387904L}; | |
251 | |
252 static uint64_t pow4_T[33]={3*1L, 3*4L, 3*16L, 3*64L, 3*256L, 3*1024L, 3*4096L, 3*16384L, 3*65536L, | |
253 (uint64_t)3*262144L, (uint64_t) 3*1048576L, (uint64_t)3*4194304L, (uint64_t)3*16777216L, (uint64_t)3*67108864L, (uint64_t)3*268435456L, (uint64_t)3*1073741824L, (uint64_t)3*4294967296L, | |
254 (uint64_t)3*17179869184L, (uint64_t)3*68719476736L, (uint64_t)3*274877906944L, (uint64_t)3*1099511627776L, (uint64_t)3*4398046511104L, (uint64_t)3*17592186044416L, | |
255 (uint64_t)3*70368744177664L, (uint64_t)3*281474976710656L, (uint64_t)3*1125899906842624L, (uint64_t)3*4503599627370496L, (uint64_t)3*18014398509481984L, | |
256 (uint64_t)3*72057594037927936L, (uint64_t) 3*288230376151711744L, (uint64_t) 3*1152921504606846976L, (uint64_t) 3*4611686018427387904L}; | |
257 | |
258 if(c == 'A') return 0; | |
259 if(c == 'C') return quick_pow4(n); | |
260 if(c == 'G') return pow4_G[n]; | |
261 if(c == 'T') return pow4_T[n]; | |
262 return 0; | |
263 } | |
264 | |
265 uint64_t hashOfWord(const unsigned char * word, uint32_t k){ | |
266 | |
267 uint64_t value = 0, jIdx; | |
268 for(jIdx=0;jIdx<k;jIdx++){ | |
269 value += quick_pow4byLetter(k-(jIdx+1), (char) word[jIdx]); | |
270 } | |
271 return value; | |
272 } | |
273 | |
274 | |
275 uint64_t asciiToUint64(const char *text){ | |
276 uint64_t number=0; | |
277 | |
278 for(;*text;text++){ | |
279 char digit=*text-'0'; | |
280 number=(number*10)+digit; | |
281 } | |
282 return number; | |
283 } |