magistraleinformaticanetworking:spm:sources14ottspm
Differenze
Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.
Entrambe le parti precedenti la revisioneRevisione precedenteProssima revisione | Revisione precedente | ||
magistraleinformaticanetworking:spm:sources14ottspm [14/10/2014 alle 17:32 (11 anni fa)] – Marco Danelutto | magistraleinformaticanetworking:spm:sources14ottspm [15/10/2014 alle 15:13 (11 anni fa)] (versione attuale) – Marco Danelutto | ||
---|---|---|---|
Linea 1: | Linea 1: | ||
===== Sample sources ===== | ===== Sample sources ===== | ||
- | C version with timings | + | C version with timings |
<code c++ farm.c> | <code c++ farm.c> | ||
#include < | #include < | ||
Linea 165: | Linea 165: | ||
- | C++ version with timings | + | C++ version with timings |
- | <code c++> | + | <code c++ Farm.cpp> |
#include < | #include < | ||
#include < | #include < | ||
Linea 273: | Linea 273: | ||
cout << "All workers terminated " << endl; | cout << "All workers terminated " << endl; | ||
cout << " | cout << " | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Version C++/Phtread with synchronizations wrapped into a new Queue< | ||
+ | <code c++ FarmQueue.cpp> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | class Task { | ||
+ | private: | ||
+ | int n; | ||
+ | float * v; | ||
+ | |||
+ | public: | ||
+ | Task(int n, float * v): | ||
+ | |||
+ | float * get() { | ||
+ | return v; | ||
+ | } | ||
+ | |||
+ | int length() { | ||
+ | return n; | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | template <class Task> class Queue { | ||
+ | private: | ||
+ | std:: | ||
+ | pthread_mutex_t lock; | ||
+ | public: | ||
+ | Queue() { | ||
+ | lock = PTHREAD_MUTEX_INITIALIZER; | ||
+ | } | ||
+ | |||
+ | ~Queue() { | ||
+ | // TBD | ||
+ | } | ||
+ | |||
+ | void send(Task m) { | ||
+ | pthread_mutex_lock(& | ||
+ | tasks.push(m); | ||
+ | pthread_mutex_unlock(& | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | Task receive() { | ||
+ | pthread_mutex_lock(& | ||
+ | Task t = (Task) tasks.front(); | ||
+ | tasks.pop(); | ||
+ | pthread_mutex_unlock(& | ||
+ | return t; | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | typedef struct __comms { | ||
+ | Queue< | ||
+ | Queue< | ||
+ | } COMMS; | ||
+ | |||
+ | Task f(Task x) { | ||
+ | return x; | ||
+ | } | ||
+ | |||
+ | void * body(void * x) { | ||
+ | COMMS * q = (COMMS *) x; | ||
+ | int * i = new int(); | ||
+ | |||
+ | while(true) { | ||
+ | Task t = (q-> | ||
+ | if(t.length() < 0) | ||
+ | break; // EOS | ||
+ | Task r = f(t); | ||
+ | (*i)++; | ||
+ | q-> | ||
+ | } | ||
+ | return ((void *) i); | ||
+ | } | ||
+ | |||
+ | int main(int argc, char * argv []) { | ||
+ | |||
+ | Queue< | ||
+ | Queue< | ||
+ | COMMS c; | ||
+ | c.in = &tasks; | ||
+ | c.out = &ress; | ||
+ | | ||
+ | int nw = atoi(argv[1]); | ||
+ | pthread_t * tid = new pthread_t[nw]; | ||
+ | |||
+ | for(int i=0; i<nw; i++) { | ||
+ | int ret = pthread_create(& | ||
+ | if(ret != 0) { | ||
+ | // TBD | ||
+ | cerr << "error creating thread " << i << endl; | ||
+ | } | ||
+ | } | ||
return(0); | return(0); | ||
} | } | ||
</ | </ |
magistraleinformaticanetworking/spm/sources14ottspm.1413307944.txt.gz · Ultima modifica: 14/10/2014 alle 17:32 (11 anni fa) da Marco Danelutto