Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Sadrži ovisnosti, pristupa se prethodnim iteracijama petlji, više iteracija mijenja jednu varijablu.
- 2. #include <omp.h>
- #include <iostream>
- #include <time.h>
- #include <cstdlib>
- using namespace std;
- int main(int argc, char* argv[]) {
- int n = 10500;
- //clock_t t1,t2;
- double t1, t2, t3, t4;
- int* v = new int[n];
- int** m = new int*[n];
- for(int i = 0; i < n; ++i)
- m[i] = new int[n];
- int* rezultat = new int[n];
- int zbroj = 0;
- int zbroj_serial = 0;
- srand (time(NULL));
- for(int i=0; i<n; i++){
- v[i] = rand() % 100 + 1;
- rezultat[i] = 0;
- }
- for(int i=0; i<n; i++){
- for(int j=0; j<n; j++) {
- m[i][j] = rand() % 100 + 1;
- }
- }
- omp_set_num_threads(4);
- cout << "Množač: ";
- for(int i=0; i<n; i++){
- //cout<< v[i] << "\t";
- }
- cout << "\n";
- cout << "Matrica_before" << endl;
- for(int i=0; i<n; i++){
- for(int j=0; j<n; j++) {
- //cout << m[i][j] << "\t";
- }
- //cout << "\n";
- }
- cout << "Number_niti_active: " << omp_get_num_threads() ;
- cout << "\n";
- //t1 = clock();
- t1 = omp_get_wtime();
- #pragma omp parallel for firstprivate(zbroj)
- for(int i=0; i<n; i++){
- for(int j=0; j<n; j++) {
- zbroj += v[i]*m[j][i];
- }
- rezultat[i] = zbroj;
- zbroj = 0;
- //cout << "Hi, i am nit: " << omp_get_thread_num() << ". I work with redak " << i << ". Bye!" << "\n";
- }
- //t2 = clock();
- t2 = omp_get_wtime();
- cout << "Rezultati: ";
- for(int i=0; i<n; i++){
- //cout<< rezultat[i] << "\t";
- }
- cout << "\n";
- //cout << "Vrijeme_time: " << ((float)(t2-t1))/CLOCKS_PER_SEC << "\n";
- cout << "Vrijeme_time_parallel: " << t2-t1 << " s" << "\n";
- //serijski dio
- t3 = omp_get_wtime();
- for(int i=0; i<n; i++){
- for(int j=0; j<n; j++) {
- zbroj_serial += v[i]*m[j][i];
- }
- rezultat[i] = zbroj_serial;
- zbroj_serial = 0;
- }
- //t2 = clock();
- t4 = omp_get_wtime();
- cout << "Vrijeme_time_serial: " << t4-t3 << " s" << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement