annotate IMSAME/src/commonFunctions.c @ 1:7fff03d777dd draft

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