int main(int argc, char ** argv){ /* variables declaration */ int i; int *size_of_buffer = 0; int num_of_ones = 0; /* Number of rounds that the algorithm will run */ int rounds = 0; /* buffer that master will send */ char *buffer = NULL; /* sessionC handler */ session *s; join_session(&argc, &argv, &s, "Slave3.spr"); role *Master = s->get_role(s, "Master"); /* receive size of buffer */ receive_int(Master, &size_of_buffer); printf("size of buffer is %d\n", *size_of_buffer); /* Dynamic memory allocation */ buffer = (char *) malloc( (*size_of_buffer) * sizeof(char)); /* Receive the buffer */ receive_string(Master, &buffer); /* For 100000 rounds */ while(rounds++ < 100000){ /* Compute the number of ones */ num_of_ones = number_of_ones(buffer); } printf("num of ones is %d\n", num_of_ones); /* Send the result to the master node */ send_int(Master, num_of_ones); /* Deallocate memory */ free(buffer); /* End current session */ end_session(s); return EXIT_SUCCESS; }
int main(int argc, char **argv){ /* Variables declaration */ int i; int j; /* Number of rounds to run the algorithm */ int rounds = 0; /* Total size of the array */ int total_size = (rows * columns) / participants; printf("Total amount of work is %d\n", total_size); /* the amount of work that is assigned to worker1 */ int amount_of_work = floor( (worker1 + 1) * columns / participants ) - floor( worker1 * columns / participants ); printf("amount of work is %d\n", amount_of_work); /* Session start */ session *s; join_session(&argc, &argv, &s, "worker1.spr"); role *master = s->get_role(s, "master"); /* Dynamic memory allocation of array C */ int *C = NULL; C = (int *) malloc(columns * sizeof(int)); if(C == NULL) { /* Terminate program if there is not enough memory */ fprintf(stderr, "out of memory \n"); exit(0); } /* Declaration of the main array */ int *Beta_worker1 = NULL; /* Dynamic memory allocation */ Beta_worker1 = (int *) malloc( total_size * sizeof(int) ); /* Abort if there is not enough memory */ if(Beta_worker1 == NULL){ fprintf(stderr, "out of memory\n"); exit(-1); } /* The array that will hold the results */ int *worker1_results = NULL; worker1_results = (int *) malloc( amount_of_work * sizeof(int) ); /* Fill with zeros the result array */ fill_with_zeros(worker1_results, amount_of_work); /* The size of the arrays that master waits from the workers */ size_t array_C = columns; size_t array_Beta = total_size; /* worker1 receives the arrays in order to do the computation */ recv_int_array(master, C, &array_C); recv_int_array(master, Beta_worker1, &array_Beta); /* Run for 1000 rounds */ while(rounds++ < 1000){ /* Main computation from master */ for(i = 0; i < amount_of_work; i++) for(j = 0; j < columns; j++) worker1_results[i] += Beta_worker1[i * amount_of_work + j] * C[j]; }//End of while /* worker1 sends the results to the master */ send_int_array(master, worker1_results, amount_of_work); /* End Session */ end_session(s); /* Deallocate memmory */ free(C); C = NULL; free(Beta_worker1); Beta_worker1 = NULL; free(worker1_results); worker1_results = NULL; return EXIT_SUCCESS; }
void cChannelSession::dataRX(const QVector<QByteArray> &data) { if (data.count() < 2) { qDebug() << "[ERROR]cChannelSession::dataRX-> Pocos campos... :" << data.count(); return; } int cmd = data[1].toInt(); //qDebug() << "[INFO]cChannelSession::dataRX-> Comando:" << cmd; if(cmd == CMD_JOIN_SESSION) { QString id = data[2]; emit join_session(id); } else if (cmd == CMD_SESSION_STATUS) { QString id = data[2]; bool status = data[3].toInt(); QString reason = data[4]; emit session_status(id,status,reason); } else if (cmd == CMD_GET_SESSION_USERLIST) { QString id = data[2]; emit get_session_userlist(id); } else if (cmd == CMD_SESSION_USERLIST) { QString id = data[2]; QByteArray array = data[3]; cUsersInfoList userslist(array); emit session_userlist(id,userslist); } else if (cmd == CMD_GET_CHATROOM_LIST) { QString id = data[2]; emit get_chatroom_list(id); } else if (cmd == CMD_CHATROOM_LIST) { QString id = data[2]; QVector<cChatInfo> chats; for ( int i = 3 ; i < data.count() ; i+=3 ) { cChatInfo chatroom(data[i],data[i+1],data[i+2].toInt()); chats.append(chatroom); } emit chatroom_list(id,chats); } else if(cmd == CMD_JOIN_CHATROOM) { QString id = data[2]; emit join_chatroom(id); } else if(cmd == CMD_CHATROOM_STATUS) { QString id = data[2]; bool status = data[3].toInt(); QString reason = data[4]; emit chatroom_status(id,status,reason); } else if(cmd == CMD_GET_CHATROOM_USERLIST) { QString chatroom_id = data[2]; emit get_chatroom_userlist(chatroom_id); } else if(cmd == CMD_CHATROOM_USERLIST) { QString id = data[2]; QByteArray array = data[3]; cUsersInfoList userslist(array); emit chatroom_userlist(id,userslist); } else if(cmd == CMD_CREATE_CHATROOM) { QString name = data[2]; bool persistent = data[3].toInt(); emit create_chatroom(name,persistent); } else if(cmd == CMD_CHATROOM_MSG) { QVector<cChatMsg> msgs; QString id = data[2]; for ( int i = 3 ; i < data.count() ; i+=3 ) { QDateTime timestamp; timestamp.fromMSecsSinceEpoch(data[i+2].toInt()); cChatMsg msg(data[i],data[i+1],timestamp); msgs.append(msg); } emit chatroom_msg(id,msgs); } else if(cmd == CMD_GET_FILELIST) { QString id = data[2]; emit get_file_list(id); } else if(cmd == CMD_FILELIST) { QString id = data[2]; } else if(cmd == CMD_GET_PAD_LIST) { QString id = data[2]; emit get_pad_list(id); } else if(cmd == CMD_PAD_LIST) { QString id = data[2]; QByteArray array(data[3]); cPadsInfoList padslist(array); emit pad_list(id,padslist); } else if(cmd == CMD_JOIN_PAD) { QString id = data[2]; emit join_pad(id); } else if(cmd == CMD_PAD_STATUS) { QString id = data[2]; bool status = data[3].toInt(); QString reason = data[4]; emit pad_status(id,status,reason); } else if(cmd == CMD_GET_PAD_USERLIST) { QString id = data[2]; emit get_pad_userlist(id); } else if(cmd == CMD_PAD_USERLIST) { QString id = data[2]; QByteArray array = data[3]; cUsersInfoList userslist(array); emit pad_userlist(id,userslist); } else if(cmd == CMD_GET_PAD_DOCUMENT) { QString id(data[2]); emit get_pad_document(id); } else if(cmd == CMD_PAD_DOCUMENT) { QString id(data[2]); QString padText(data[3]); emit pad_document(id,padText); } else if(cmd == CMD_PAD_CHANGES) { QString id(data[2]); QString sender = data[3]; int pos(data[4].toInt()); int del(data[5].toInt()); int add(data[6].toInt()); QString text(data[7]); emit pad_changes(id,sender,pos,del,add,text); } else { qDebug() << "[ERROR]cChannelSession::dataRX-> Comando desconocido:" << cmd; } }
/* Main function */ int main(int argc, char **argv){ /* Variables declaration */ int i; int j; /* The number of non zero elements */ int *num_of_no_zeros = NULL; /* number of rounds */ int rounds = 0; /* Session start */ session *s; join_session(&argc, &argv, &s, "worker3.spr"); role *Master = s->get_role(s, "Master"); printf("I am here\n"); /* Receive number of non zero elements */ receive_int(Master, &num_of_no_zeros); printf("NUm of zeros is %d\n", *num_of_no_zeros); /* Number of non zero elements */ int no_zeros = *num_of_no_zeros; /* Declaration and dynamic memory allocation af array row_ptr */ int *row_ptr = (int *) malloc((nrows + 1) * sizeof(int)); if(row_ptr == NULL){ fprintf(stderr, "Out of memory, aborting program...\n"); exit(-1); } /* Decalration and dynamic memory allocation af array values */ int *values = (int *) malloc(no_zeros * sizeof(int)); if(values == NULL){ fprintf(stderr, "Out of memory, aborting program...\n"); exit(-1); } /* Declaration and dynamic memory allocation af col_ind array */ int *col_ind = (int *) malloc(no_zeros * sizeof(int)); if(col_ind == NULL){ fprintf(stderr, "Out of memory, aborting program...\n"); exit(-1); } /* Declaration and dynamic memory allocation af x array */ int *x = (int *) malloc(ncolumns * sizeof(int)); if(x == NULL){ fprintf(stderr, "Out of memory, aborting program...\n"); exit(-1); } /* Expected size of array */ size_t sz_rows = 1001; size_t sz_cols = 1000; size_t sz_no_zeros = no_zeros; /* Receive row_ptr from master */ recv_int_array(Master, row_ptr, &sz_rows); /* Receive values array from master */ recv_int_array(Master, values, &sz_no_zeros); /* Receive col_ind array from master */ recv_int_array(Master, col_ind, &sz_no_zeros); /* Receive x array from master */ recv_int_array(Master, x, &sz_cols); printf("value[%d] = %d\n", 0, values[0]); /* The array that will hold the results of the main computation */ int *results = NULL; /* Define the amount of work that worker 1 will do */ int start_work; int end_work; int amount_of_work; /* The amount of work that the master will do */ start_work = floor((worker3 * nrows)/participants) / 10; end_work = floor(((worker3 + 1) * nrows)/participants) / 10; amount_of_work = (end_work - start_work); printf("start = %d - end = %d - amount of work = %d\n", start_work, end_work, amount_of_work); /* Declaration and dynamic memory allocation af x array */ results = (int *) malloc( 2 * amount_of_work * sizeof(int)); if(results == NULL){ fprintf(stderr, "Out of memory, aborting program...\n"); exit(-1); } /* Run for 100 rounds */ while(rounds++ < 10000){ /* Main computation of the result. Worker1 computes the work that is assigned to it*/ for(i = start_work; i < end_work; i++){ for(j = row_ptr[i]; j < row_ptr[i + 1]; j++) results[i] += values[j] * x[col_ind[j]]; } } int array[amount_of_work]; for(i = 0; i < amount_of_work; i++) array[i] = 0; //send_int_array(Master, array, amount_of_work); //printf("here i am \n"); /* Send the results to the master */ //send_int_array(Master, values, amount_of_work); send_int_array(Master, results, amount_of_work); //send_int(Master, amount_of_work); //recv_int_array(Master, col_ind, &sz_no_zeros); printf("gamw!!!\n"); /* End current session */ end_session(s); /* Free memory */ free(row_ptr); row_ptr = NULL; free(values); values = NULL; free(col_ind); col_ind = NULL; free(x); x = NULL; free(results); results = NULL; return EXIT_SUCCESS; }