1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
//get all the samples and sum them to stdout
//make zstd=1
//gcc -Wall -O2 -I include/ -o get_all_samples test/bench/get_all_samples.c lib/libslow5.a python/slow5threads.c -lm -lz -lzstd -lpthread -fopenmp
#include <stdio.h>
#include <stdlib.h>
#include <slow5/slow5.h>
#include <omp.h>
#include <sys/time.h>
#include <pthread.h>
#include "../../python/slow5threads.h"
#include "../../src/slow5_extra.h"
int threads = 10;
/* argument wrapper for multithreaded framework used for input/processing/output interleaving */
typedef struct {
int batch_size;
slow5_rec_t **slow5_rec;
uint64_t *sums;
char **mem_records;
size_t *mem_bytes;
slow5_file_t *sp;
FILE *fp;
//conditional variable for notifying the processing to the output threads
pthread_cond_t cond;
pthread_mutex_t mutex;
int8_t finished;
} pthread_arg_t;
static inline double realtime(void) {
struct timeval tp;
struct timezone tzp;
gettimeofday(&tp, &tzp);
return tp.tv_sec + tp.tv_usec * 1e-6;
}
/* load a data batch from disk */
int load_raw_batch(char ***mem_records_a, size_t **mem_bytes_a, slow5_file_t *sf, int batch_size) {
char **mem_records = (char **)malloc(batch_size * sizeof(char *));
size_t *mem_bytes = (size_t *)malloc(batch_size * sizeof(size_t));
int32_t i = 0;
while (i < batch_size) {
mem_records[i] = (char *)slow5_get_next_mem(&(mem_bytes[i]), sf);
if (mem_records[i] == NULL) {
if (slow5_errno != SLOW5_ERR_EOF) {
fprintf(stderr,"Error reading from SLOW5 file %d\n", slow5_errno);
exit(EXIT_FAILURE);
}
else {
break;
}
}
else {
i++;
}
}
*mem_records_a = mem_records;
*mem_bytes_a = mem_bytes;
return i;
}
void free_raw_batch(char **mem_records, size_t *mem_bytes, int batch_size) {
for(int i=0;i<batch_size;i++) {
free(mem_records[i]);
}
free(mem_records);
free(mem_bytes);
}
void* process_batch(void* voidargs) {
pthread_arg_t* args = (pthread_arg_t*)voidargs;
int batch_size = args->batch_size;
slow5_rec_t **slow5_rec = (slow5_rec_t**)calloc(batch_size,sizeof(slow5_rec_t*));
uint64_t *sums = (uint64_t*)malloc(batch_size*sizeof(uint64_t));
char **mem_records = args-> mem_records;
size_t *mem_bytes = args->mem_bytes;
slow5_file_t *sp = args->sp;
//fprintf(stderr,"Processing started for %d records\n", batch_size);
omp_set_num_threads(threads);
#pragma omp parallel for
for(int i=0;i<batch_size;i++){
if(slow5_rec_depress_parse(&mem_records[i], &mem_bytes[i], NULL, &slow5_rec[i], sp)!=0){
fprintf(stderr,"Error parsing the record %s",slow5_rec[i]->read_id);
exit(EXIT_FAILURE);
}
uint64_t sum = 0;
for(int j=0; j<slow5_rec[i]->len_raw_signal; j++){
sum += slow5_rec[i]->raw_signal[j];
}
sums[i] = sum;
}
args->slow5_rec = slow5_rec;
args->sums = sums;
pthread_mutex_lock(&args->mutex);
pthread_cond_signal(&args->cond);
args->finished=1;
pthread_mutex_unlock(&args->mutex);
//fprintf(stderr,"batch processed with %d reads\n",batch_size);
pthread_exit(0);
}
void* output_free_batch(void* voidargs){
pthread_arg_t* args = (pthread_arg_t*)voidargs;
//fprintf(stderr,"Waiting till processor complete\n");
pthread_mutex_lock(&args->mutex);
while(args->finished==0){
pthread_cond_wait(&args->cond, &args->mutex);
}
pthread_mutex_unlock(&args->mutex);
//fprintf(stderr,"processor completed\n");
int batch_size = args->batch_size;
slow5_rec_t **slow5_rec = args->slow5_rec;
uint64_t *sums = args->sums;
char **mem_records = args-> mem_records;
size_t *mem_bytes = args->mem_bytes;
FILE *fp = args->fp;
for(int i=0;i<batch_size;i++){
fprintf(fp,"%s,%ld\n",slow5_rec[i]->read_id,sums[i]);
}
//fprintf(stderr,"batch printed with %d reads\n",batch_size);
free_raw_batch(mem_records, mem_bytes, batch_size);
for(int i=0;i<batch_size;i++){
slow5_rec_free(slow5_rec[i]);
}
free(slow5_rec);
free(sums);
pthread_cond_destroy(&args->cond);
pthread_mutex_destroy(&args->mutex);
free(args);
pthread_exit(0);
}
int main(int argc, char *argv[]) {
if(argc != 5) {
fprintf(stderr, "Usage: %s in_file.blow5 out_file.csv num_thread batch_size\n", argv[0]);
return EXIT_FAILURE;
}
int batch_size = atoi(argv[4]);
int num_thread = atoi(argv[3]);
int ret=batch_size;
threads = num_thread;
FILE *fp = fopen(argv[2],"w");
if(fp==NULL){
fprintf(stderr,"Error in opening file %s for writing\n",argv[2]);
perror("perr: ");
exit(EXIT_FAILURE);
}
fputs("read_id,samples\n", fp);
double tot_time = 0;
double t0 = realtime();
slow5_file_t *sp = slow5_open(argv[1],"r");
if(sp==NULL){
fprintf(stderr,"Error in opening file\n");
perror("perr: ");
exit(EXIT_FAILURE);
}
tot_time += realtime() - t0;
int8_t first_flag_p=0;
int8_t first_flag_pp=0;
pthread_t tid_p;
pthread_t tid_pp;
while(ret == batch_size){
t0 = realtime();
char **mem_records = NULL;
size_t *mem_bytes = NULL;
ret = load_raw_batch(&mem_records, &mem_bytes, sp, batch_size);
tot_time += realtime() - t0;
//fprintf(stderr,"batch loaded with %d reads\n",ret);
if(first_flag_p){
if(pthread_join(tid_p, NULL) < 0){
fprintf(stderr,"Error in joining thread\n");
exit(EXIT_FAILURE);
}
}
first_flag_p=1;
pthread_arg_t *pt_arg = (pthread_arg_t*)malloc(sizeof(pthread_arg_t));
pt_arg->batch_size = ret;
pt_arg->mem_records = mem_records;
pt_arg->mem_bytes = mem_bytes;
pt_arg->sp = sp;
pt_arg->fp = fp;
pt_arg->finished = 0;
if (pthread_cond_init(&pt_arg->cond, NULL) < 0){
fprintf(stderr,"Error in initializing condition variable\n");
exit(EXIT_FAILURE);
}
if (pthread_mutex_init(&pt_arg->mutex, NULL) < 0){
fprintf(stderr,"Error in initializing mutex\n");
exit(EXIT_FAILURE);
}
if(pthread_create(&tid_p, NULL, process_batch,
(void*)(pt_arg)) < 0) {
fprintf(stderr,"Error in creating thread\n");
exit(EXIT_FAILURE);
}
if(first_flag_pp){
if(pthread_join(tid_pp, NULL) < 0){
fprintf(stderr,"Error in joining thread\n");
exit(EXIT_FAILURE);
}
}
first_flag_pp=1;
if(pthread_create(&tid_pp, NULL, output_free_batch,
(void*)(pt_arg)) < 0){
fprintf(stderr,"Error in creating thread\n");
exit(EXIT_FAILURE);
}
}
if(pthread_join(tid_p, NULL) < 0){
fprintf(stderr,"Error in joining thread\n");
exit(EXIT_FAILURE);
}
if(pthread_join(tid_pp, NULL) < 0){
fprintf(stderr,"Error in joining thread\n");
exit(EXIT_FAILURE);
}
t0 = realtime();
slow5_close(sp);
tot_time += realtime() - t0;
fclose(fp);
fprintf(stderr,"Time for getting raw bytes (exclude depress & parse) %f\n", tot_time);
return 0;
}
|