Note that there are some explanatory texts on larger screens.

plurals
  1. POC: performance of pthread, low than single thrad
    primarykey
    data
    text
    <p>I'm confusing about the performance of my code, when dealing with single thread it only using 13s, but it's will consume 80s. I don't know whether the vector can only be accessed by one thread at a time, if so it's likely I have to use a struct array to store data instead of vector, could anyone kindly help?</p> <pre><code>#include &lt;iostream&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;vector&gt; #include &lt;iterator&gt; #include &lt;string&gt; #include &lt;ctime&gt; #include &lt;bangdb/database.h&gt; #include "SEQ.h" #define NUM_THREADS 16 using namespace std; typedef struct _thread_data_t { std::vector&lt;FDT&gt; *Query; unsigned long start; unsigned long end; connection* conn; int thread; } thread_data_t; void *thr_func(void *arg) { thread_data_t *data = (thread_data_t *)arg; std::vector&lt;FDT&gt; *Query = data-&gt;Query; unsigned long start = data-&gt;start; unsigned long end = data-&gt;end; connection* conn = data-&gt;conn; printf("thread %d started %lu -&gt; %lu\n", data-&gt;thread, start, end); for (unsigned long i=start;i&lt;=end ;i++ ) { FDT *fout = conn-&gt;get(&amp;((*Query).at(i))); if (fout == NULL) { //printf("%s\tNULL\n", s); } else { printf("Thread:%d\t%s\n", data-&gt;thread, fout-&gt;data); } } pthread_exit(NULL); } int main(int argc, char *argv[]) { if (argc&lt;2) { printf("USAGE: ./seq &lt;.txt&gt;\n"); printf("/home/rd/SCRIPTs/12X18610_L5_I052.R1.clean.code.seq\n"); exit(-1); } printf("%s\n", argv[1]); vector&lt;FDT&gt; Query; FILE* fpin; if((fpin=fopen(argv[1],"r"))==NULL) { printf("Can't open Input file %s\n", argv[1]); return -1; } char *key = (char *)malloc(36); while (fscanf(fpin, "%s", key) != EOF) { SEQ * sequence = new SEQ(key); FDT *fk = new FDT( (void*)sequence, sizeof(*sequence) ); Query.push_back(*fk); } unsigned long Querysize = (unsigned long)(Query.size()); std::cout &lt;&lt; "myvector stores " &lt;&lt; Querysize &lt;&lt; " numbers.\n"; //create database, table and connection database* db = new database((char*)"berrydb"); //get a table, a new one or existing one, walog tells if log is on or off table* tbl = db-&gt;gettable((char*)"hg19", JUSTOPEN); if(tbl == NULL) { printf("ERROR:table NULL error"); exit(-1); } //get a new connection connection* conn = tbl-&gt;getconnection(); if(conn == NULL) { printf("ERROR:connection NULL error"); exit(-1); } cerr&lt;&lt;"begin querying...\n"; time_t begin, end; double duration; begin = clock(); unsigned long ThreadDealSize = Querysize/NUM_THREADS; cerr&lt;&lt;"Querysize:"&lt;&lt;ThreadDealSize&lt;&lt;endl; pthread_t thr[NUM_THREADS]; int rc; thread_data_t thr_data[NUM_THREADS]; for (int i=0;i&lt;NUM_THREADS ;i++ ) { unsigned long ThreadDealStart = ThreadDealSize*i; unsigned long ThreadDealEnd = ThreadDealSize*(i+1) - 1; if (i == (NUM_THREADS-1) ) { ThreadDealEnd = Querysize-1; } thr_data[i].conn = conn; thr_data[i].Query = &amp;Query; thr_data[i].start = ThreadDealStart; thr_data[i].end = ThreadDealEnd; thr_data[i].thread = i; } for (int i=0;i&lt;NUM_THREADS ;i++ ) { if (rc = pthread_create(&amp;thr[i], NULL, thr_func, &amp;thr_data[i])) { fprintf(stderr, "error: pthread_create, rc: %d\n", rc); return EXIT_FAILURE; } } for (int i = 0; i &lt; NUM_THREADS; ++i) { pthread_join(thr[i], NULL); } cerr&lt;&lt;"done\n"&lt;&lt;endl; end = clock(); duration = double(end - begin) / CLOCKS_PER_SEC; cerr &lt;&lt; "runtime: " &lt;&lt; duration &lt;&lt; "\n" &lt;&lt; endl; db-&gt;closedatabase(OPTIMISTIC); delete db; printf("Done\n"); return EXIT_SUCCESS; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload