File: random_read_pthreads.c

package info (click to toggle)
libslow5lib 0.7.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 25,092 kB
  • sloc: ansic: 11,825; python: 1,179; sh: 547; makefile: 91; cpp: 40
file content (93 lines) | stat: -rw-r--r-- 2,593 bytes parent folder | download | duplicates (2)
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
// an example programme that uses slow5lib to randomly access records in a SLOW5 file using multiple threads (pthreads)

#include <stdio.h>
#include <stdlib.h>
#include <slow5/slow5.h>
#include <pthread.h>
#include <errno.h>

#define FILE_PATH "examples/example.slow5"
#define NUM_THREADS 4
#define READ_LIST_SIZE 4  //for simplicity number of threads are equal to number of reads to access, we assign one thread for each access

char * read_id_list[READ_LIST_SIZE] = {"r4", "r1", "r3", "r4"};

/* arguments for a thread */
typedef struct {
    slow5_file_t *sp;
    char *read_id;
} pthread_arg;

/* thread function */
void* access_read(void* voidargs) {

    pthread_arg *args = (pthread_arg*)voidargs; //cast the void pointer to pthread_arg pointer
    slow5_rec_t *rec = NULL; //slow5 record to be read

    //fetch the read with read_id assigned to this thread
    int ret = slow5_get(args->read_id, &rec, args->sp);
    if (ret < 0) {
        fprintf(stderr, "Error when when fetching the read %s\n",args->read_id);
    } else {
        fprintf(stderr, "Successfully fetched the read %s with %ld raw signal samples\n", rec->read_id, rec->len_raw_signal);
        // .... do the processing here as needed
    }

    //free the SLOW5 record
    slow5_rec_free(rec);

    pthread_exit(0);
}


int main(){

    //open the SLOW5 file
    slow5_file_t *sp = slow5_open(FILE_PATH,"r");
    if(sp==NULL){
        fprintf(stderr,"Error in opening file\n");
        exit(EXIT_FAILURE);
    }

    int ret,t = 0; //for return value and thread index

    //load the SLOW5 index (will be built if not present)
    ret = slow5_idx_load(sp);
    if (ret < 0) {
        fprintf(stderr, "Error in loading index\n");
        exit(EXIT_FAILURE);
    }

    //thread IDs and arguments
    pthread_t tids[NUM_THREADS];
    pthread_arg args[NUM_THREADS];

    //create threads
    for(t = 0; t < NUM_THREADS; t++){
        args[t].sp = sp; //assign the SLOW5 file pointer
        args[t].read_id = read_id_list[t]; //assign a read_id to each thread

        ret = pthread_create(&tids[t], NULL, access_read, (void*)(&args[t]));
        if(ret < 0){
            fprintf(stderr, "Error creating thread\n");
            exit(EXIT_FAILURE);
        }
    }

    //pthread joining
    for (t = 0; t < NUM_THREADS; t++) {
        int ret = pthread_join(tids[t], NULL);
        if(ret < 0){
            fprintf(stderr, "Error creating thread\n");
            exit(EXIT_FAILURE);
        }
    }

    //free the SLOW5 index
    slow5_idx_unload(sp);

    //close the SLOW5 file
    slow5_close(sp);

    return  0;
}