Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
add debugging code
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Van der Jeugt committed Nov 30, 2018
1 parent 376e10e commit 42f73b1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
17 changes: 15 additions & 2 deletions hmm.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,22 @@ const char *translation_table;
/** The translation table to use for anti-codon translation */
const char *translation_table_rc;

/* Macro to have easy debug messages */
#define log_debug(...) \
do { \
if (verbose) { \
fprintf(stdout, "[Debug] %s (line %d): ", __FILE__, __LINE__); \
fprintf(stdout, __VA_ARGS__); \
} \
} while (0)

// semaphores
#ifdef __APPLE__
typedef sem_t* SEM_T;
#elif __linux
typedef sem_t SEM_T;
#define sem_wait(x) sem_wait(&x)
#define sem_post(x) sem_post(&x)
#define sem_wait(x) log_debug("waiting %s\n", #x);sem_wait(&x);log_debug("got %s\n", #x)
#define sem_post(x) log_debug("posting %s\n", #x);sem_post(&x);log_debug("put %s\n", #x)
#endif

/**
Expand All @@ -146,6 +155,10 @@ SEM_T sema_Q;
* before it's fully read.
*/
SEM_T sema_R;
/**
* Protects the boolean indicating all input is read
*/
SEM_T sema_F;

SEM_T sema_r;
SEM_T sema_w;
Expand Down
27 changes: 17 additions & 10 deletions run_hmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,6 @@ FILE *dna_outfile_fp;
/** The nr of sequences the writer thread has outputted */
long writer_counter = 0;

/* Macro to have easy debug messages */
#define log_debug(...) \
do { \
if (verbose) { \
fprintf(stdout, "[Debug] %s (line %d): ", __FILE__, __LINE__); \
fprintf(stdout, __VA_ARGS__); \
} \
} while (0)

void parseArguments(int argc, char **argv) {
/* read command line argument */
//!! This argument reading should all be encapsulated in a single function, this will make reading the code much easier, right now we have to always move around it.
Expand Down Expand Up @@ -228,11 +219,13 @@ void initializeSemaphores() {
#ifdef __APPLE__
sem_unlink("/sema_Q");
sem_unlink("/sema_R");
sem_unlink("/sema_F");
sem_unlink("/sema_r");
sem_unlink("/sema_w");

if ((sema_Q = sem_open("/sema_Q", O_CREAT, 0644, 1)) == SEM_FAILED ||
(sema_R = sem_open("/sema_R", O_CREAT, 0644, 1)) == SEM_FAILED ||
(sema_F = sem_open("/sema_F", O_CREAT, 0644, 1)) == SEM_FAILED ||
(sema_r = sem_open("/sema_r", O_CREAT, 0644, 1)) == SEM_FAILED ||
(sema_w = sem_open("/sema_w", O_CREAT, 0644, 1)) == SEM_FAILED) {
perror("ERROR: sem_open");
Expand All @@ -242,6 +235,7 @@ void initializeSemaphores() {
#elif __linux
sem_init(&sema_Q, 0, 1);
sem_init(&sema_R, 0, 1);
sem_init(&sema_F, 0, 1);
sem_init(&sema_r, 0, 0);
sem_init(&sema_w, 0, 0);
#endif
Expand All @@ -252,6 +246,7 @@ void destroySemaphores() {
#ifdef __APPLE__
sem_unlink("/sema_Q");
sem_unlink("/sema_R");
sem_unlink("/sema_F");
sem_unlink("/sema_r");
sem_unlink("/sema_w");

Expand All @@ -268,6 +263,7 @@ void destroySemaphores() {
#elif __linux
sem_destroy(&sema_Q);
sem_destroy(&sema_R);
sem_destroy(&sema_F);
sem_destroy(&sema_r);
sem_destroy(&sema_w);
#endif
Expand Down Expand Up @@ -331,8 +327,11 @@ void readerThread() {
stopped_at_fpos = read_seq_into_buffer(fp, temp->td, temp->buffer, false);

/* We've fully read the input sequence file */
if (stopped_at_fpos == 0)
if (stopped_at_fpos == 0) {
sem_wait(sema_F);
num_reads_flag = true;
sem_post(sema_F);
}

sem_post(sema_R);

Expand All @@ -343,7 +342,11 @@ void readerThread() {

log_debug("Finished handing out all the work...\n");
fasta_file_free(fp);

sem_wait(sema_F);
sem_post(sema_w); /* ensure it doesn't block */
num_reads_flag = true;
sem_post(sema_F);
}

int main (int argc, char **argv) {
Expand Down Expand Up @@ -552,10 +555,12 @@ void closeFilePointers( FILE **aa_outfile_fp, FILE **outfile_fp, FILE **dna_outf
void *writerThread(void *args) {
FILE *aa_outfile_fp = openFilePointers();

sem_wait(sema_F);
while (true) {
QUEUE *temp;

sem_wait(sema_w);
sem_post(sema_F);

/* Fetch the queue of worker threads that are done */
sem_wait(sema_Q);
Expand All @@ -577,9 +582,11 @@ void *writerThread(void *args) {
}

/* Check if we're done, i.e. the whole input was read and processed*/
sem_wait(sema_F);
if (num_reads_flag && writer_counter == read_counter)
break;
}
sem_post(sema_F);

/* We're done writing, so close the output files */
closeFilePointers(&aa_outfile_fp, &outfile_fp, &dna_outfile_fp );
Expand Down

0 comments on commit 42f73b1

Please sign in to comment.