Not sure how to get started on threading with matrixes in PThreads
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Not sure how to get started on threading with matrixes in PThreads

  1. #1

    Not sure how to get started on threading with matrixes in PThreads

    I'm supposed to use C++ and the PThread API (can't use the Win 32 API). I'm supposed to take a matrix and use a bunch of threads to solve each part of the matrix, or each calculation. I'm to have bunch of threads or process, can't recall which. Anyway, I need them to solve each part of the matrix, but only do one calculation per thread.

    I'm to read in two text files, each containing a matrix, and then use a bunch of threads, one per calculation, to calculate the new matrix and then am to write the new matrix to the screen.

    I have little idea how to read in each matrix.

    The main thread, yeah, it's threads, is supposed to initalize the matrixes, with the values of the text files I think.

    Another problem is that I'm not allowed to assume the size of the matrices, yet I'll need to know their sizes somehow in order to do the calculations correctly. I already have the formula for the calculations, and, once this gets to work, should be able to run it for each thread.

    I think I need to somehow tell it to add some int variable that keeps track of the X size, and the y size. It need not be a N by N matrix that's read in. However, I'm going to assume that it has the same number of elements in each row (or else I'm doomed and I don't think it'd be a proper matrix if it did that anyway). I can tell it to increase the x size, but only for the first row and only until it reaches a new line, and that's another thing I'm confused about what to do.

    For instance:

    1 2 3 4

    Should have a size of 4

    However, I don't want it, if it's counting spaces, to see five spaces and mistakenly assume a size of 5.

    Also, I don't know how to tell it if somehow the input file might be

    1 2 3 4

    to not suddenly assume 8 items are there now.

    First off, how do I get it to read it in and count how many numbers are in a row and how many columns there are?

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    iostream reader, reader2;
    
    int main(int var, char* args)
    {
    
    
    reader(args[1]);
    
    int rows = 0;
    
    // need to read in till it reach a new line character.  Need to also increment 
    // rows every time a space is reached, but not to keep incrementing if there happens to be two // or more spaces in the break.  
    
    
    
    
    }
    I'm trying to figure out how to make sure it can read in the info at the right time, as it doesn't say what to do with it, other than print it out after the calculations have been made.

  2. #2

    Re: Not sure how to get started on threading with matrixes in PThreads

    Ok, I've updated it a little bit.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include<vector>
    
       using namespace std;
    
       class Matrix
       {
       
       /* structure for passing data to threads */
       
          struct v
          {
             int i; /* row */
             int j; /* column */
          };
       
       
          iostream reader, reader2;
          vector<vector<int> > A;
          vector<vector<int> > B;
       
       
          int main(int var, char* args)
          {
          
          
          
             reader(args[1]);
          
             int rows = 0;
          
          // need to read in till it reach a new line character.  Need to also increment 
          // rows every time a space is reached, but not to keep incrementing if there happens to be two // or more spaces in the break.  
          
          
          
          // After the size of A and B have been determined
          
          /* We have to create M * N worker threads */
          
             int M = A.size();
             int N = B[0].size();
          
             for (i = 0; i < M; i++)
             {
                for (j = 0; j < N; j++)
                {
                   struct v *data = (struct v *) malloc(sizeof(struct v));
                
                
                
                }
             }
          }
       
          void run(int i, int j)
          {
          
          // Calculates the matrix product for C(i,j)
          
          }
       
       
       };
    C i,j = Sum of (from n =1 to K) A i,n * B n,j

    C is the matrix that is made from the cross products of A and B as shown above.

    I'm to use the

    I think that I'm later to join the threads using a code like this, where the size is M * N.

    Code:
    /* an array of threads to be joined upon */
    
    pthread_t workers[M*N];
    
    for (int i =0; i < (M * N); i++)
    {
    pthread_join(workers[i], NULL);
    }

    Also, I have this code that can read it in for one line, but I want it to handle multiple lines. But I don't know how. This will enable me to read in the data from the matrixes and store them in vector of vector of int.

    However, I don't know what to put for multiple lines. I'm thinking I need some outer while loop but don't know what to put in it.

    Code:
    #include <iostream>
    #include <istream>
    #include <fstream>
    #include <vector>
    
    
       using namespace std;
    
       int main(int argc, char* argv[])
       {
          int size;
          vector<int> nums;
       
       
          fstream fin;
       
       
          fin.open(argv[1]);
       
          int temp;
       
       // reader >> temp;
       
       // nums.push_back(temp);
       
       // fin >> temp;
       //nums.push_back(temp);
       
       
          while (fin >> temp)
          {
          
             nums.push_back(temp);
          
          }
        
        
       
          fin.close();
       
          for (int i =0; i < nums.size(); i++)
          {
          
             cout << nums[i]  << " ";
          }
          cout << "\n" << nums.size();
       
       }
    It's being used to get the data to use for the threads. Storing them seemed like the only idea that made sense.

    I tried this code below to try and read in multiple lines and store it in a vector of vectors, but, though it compiles and runs, is printing out nothing.

    Code:
    #include <iostream>
    #include <istream>
    #include <fstream>
    #include <vector>
    
    
       using namespace std;
    
       int main(int argc, char* argv[])
       {
          int size;
          vector<vector<int> > nums;
       
       
          fstream fin;
       
       
          fin.open(argv[1]);
       
          int temp;
       
       // reader >> temp;
       
       // nums.push_back(temp);
       
       // fin >> temp;
       //nums.push_back(temp);
       
       
          while (fin >> temp)
          {
             nums.push_back(vector<int>());
             while (!fin.peek() == '\n')
             {
                nums[size].push_back(temp);
             
             }
             size++;
          }
        
        
       
          fin.close();
       
          for (int i =0; i < nums.size(); i++)
          {
             for (int j =0; j < nums[i].size(); j++)
             {
                cout << nums[i][j]  << " ";
             }
             cout << "\n";
          }
          cout << "\n" << nums.size();
       
       }
    What it appears to be doing is printing out the total number of numbers. Not the numbers in each one.

    It's my (while !fin.peek() == '\n')
    that it doesn't like but what should I have instead?

    I've tried this and am getting an infinite loop.

    Code:
    #include <iostream>
    #include <istream>
    #include <fstream>
    #include <vector>
    
    
       using namespace std;
    
       int main(int argc, char* argv[])
       {
          int size = 0;
          vector<vector<int> > nums;
       
       
          fstream fin;
       
          cout << "Hey!";
          fin.open(argv[1]);
       
          int temp = 0;
       
       // reader >> temp;
       
       // nums.push_back(temp);
       
       // fin >> temp;
       //nums.push_back(temp);
          int oops;
       
          while (fin >> temp)
          {
             cout << "Hey!";
             nums.push_back(vector<int>());
          // cout << nums.size();
             cout << size;
          //return 1;
             cout << oops;
             oops++;
          
          //cout << fin.peek();
          
             if (temp !='\n')
             {
                while (temp != '\n')
                {
                   cout <<"Went here.";
                   cout << temp;
                   cout << size;
                
                
                   nums[size].push_back(temp);
                
                }
             }
             size++;
          
          }
        
        
       
          fin.close();
       
       
       
          for (int i =0; i < nums.size(); i++)
          {
             for (int j =0; j < nums[i].size(); j++)
             {
                cout << nums[i][j]  << " ";
             }
             cout << "\n";
          }
          cout << "\n" << nums.size();
          cout << "\n";
          cout << nums[0].size();
       
       }
    Last edited by jedipenguin; March 24th, 2012 at 07:14 PM.

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,014

    Re: Not sure how to get started on threading with matrixes in PThreads

    Quote Originally Posted by jedipenguin View Post
    I'm supposed to use C++ and the PThread API (can't use the Win 32 API). I'm supposed to take a matrix and use a bunch of threads to solve each part of the matrix, or each calculation. I'm to have bunch of threads or process, can't recall which. Anyway, I need them to solve each part of the matrix, but only do one calculation per thread.
    You should learn enough C++ to be able to write this as a single-threaded program before you start using threads. Which book/method are you using to learn C++?
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  4. #4
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Not sure how to get started on threading with matrixes in PThreads

    Quote Originally Posted by jedipenguin View Post
    I tried this code below to try and read in multiple lines and store it in a vector of vectors, but, though it compiles and runs, is printing out nothing.
    I will be very honest with you -- you are attempting to run a marathon, but haven't learned to walk.

    In other words, writing a threaded program requires you to know the language you're using to write the threaded program. When you're learning about multithreaded programming, you shouldn't be at the stage where you're fighting to learn the basic fundamentals of the language you're using.

    If you don't know C++ well enough to write a simple loop using vectors with no threads, then attempting to write a multithreaded program will be a futile attempt.

    So D_Drmmr gives good advice -- learn C++ properly first. It is hard enough to learn the ins and outs of multithreaded programming, even if you were a whiz at C++.

    Regards,

    Paul McKenzie

  5. #5

    Re: Not sure how to get started on threading with matrixes in PThreads

    I'm afraid I don't have that luxury. I did have one C++ course before, though it was taught rather fast pace and now the university is making there be a prerequisite that goes over C++ more, but that prereq wasn't available when I took the other course.

    Also, this is due in two days so trying to really master C++ in that time is very futile. Anyway, I got the readin part to almost work, though it's adding in an empty vector to the vector of vectors for some odd reason.

    Also, it's just basic multithreading.

    They had a lot of the code shown in the book.

    What confuses me in C++ is unfamiliarity with the data structures and import types and also the pointer thing.

    Code:
    #include <iostream>
    #include <istream>
    #include <fstream>
    #include <vector>
    #include <sstream>
    
    
       using namespace std;
    
      class Readin
      {
      
      struct v
      {
      int i;
      int j;
      }
       int main(int argc, char* argv[])
       {
          int size = 0;
          vector<vector<int> > nums;
       
       
          fstream fin;
       
       
          fin.open(argv[1]);
       
          int temp = 0;
       
       // reader >> temp;
       
       // nums.push_back(temp);
       
       // fin >> temp;
       //nums.push_back(temp);
       
       string line;
          while (getline(fin, line))
          {
          
             
         istringstream tempStream(line);
    
         vector<int> tempVec;
    
         int temp2;
         while( tempStream >> temp2 )
         {
              tempVec.push_back(temp2);
         }
           nums.push_back(tempVec);
          }
        
        
       
          fin.close();
       
          for (int i =0; i < nums.size(); i++)
          {
             for (int j =0; j < nums[i].size(); j++)
             {
                cout << nums[i][j]  << " ";
             }
             cout << "\n";
          }
          cout << nums[0].size();
          cout << nums[1].size();
          cout << nums[2].size();
          cout << "\n" << nums.size();
       return 0;
       }
       };

  6. #6
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Not sure how to get started on threading with matrixes in PThreads

    Quote Originally Posted by jedipenguin View Post
    Also, it's just basic multithreading.
    That's the issue -- multithreading is hardly "basic". To let you know, many companies will refuse to hire any programmers who have no experience in multithreaded programming, regardless of how much skill and expertise they have writing single-threaded programs.

    So if it's that basic, then any single-threaded programmer should be able to learn multithreaded programming quickly. Unfortunately, that is not the case most of the time.

    Try this code:
    Code:
    #include <vector>
    #include <iostream>
    
    typedef std::vector<int> IntArray;
    typedef std::vector<IntArray> IntArray2D;
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
        IntArray2D nums;
        IntArray myData;
    
        for (int i = 0; i < 112; ++i)
        {
            if ((i &#37; 3) == 0 && i != 0 )
            {
                nums.push_back( myData );
                myData.clear();
            }
            myData.push_back( i );    
        }
    
        for (int i =0; i < nums.size(); i++)
        {
            for (int j =0; j < nums[i].size(); j++)
            {
                cout << nums[i][j]  << " ";
            }
            cout << "\n";
        }
        cout << nums[0].size() << "\n";
        cout << nums[1].size() << "\n";
        cout << nums[2].size() << "\n";
        cout << "\n" << nums.size();
        return 0;
    }
    First, understand how to store numbers in a 2 dimensional vector. It doesn't matter if the numbers come from a file, the keyboard, or generated as in the example above.

    Regards,

    Paul McKenzie

  7. #7

    Re: Not sure how to get started on threading with matrixes in PThreads

    I added a statement to tell it not to add any empty vectors. However, when I told it to print out nums[2].size(), despite there not even being such a vector, well there was one that had nothing in it but my if statement I added kept it out and it now says the size of the vector of vectors is 2. However, nums[2].size() is returning 4. How is that?

  8. #8
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Not sure how to get started on threading with matrixes in PThreads

    Quote Originally Posted by jedipenguin View Post
    I added kept it out and it now says the size of the vector of vectors is 2. However, nums[2].size() is returning 4. How is that?
    If there are 2 items in the vector, the valid entries are num[0] and num[1]. There is no num[2].

    So what you did was enter the land of undefined behaviour by accessing an element in the vector beyond the boundaries of the vector.

    Regards,

    Paul McKenzie

  9. #9

    Re: Not sure how to get started on threading with matrixes in PThreads

    Now I'm getting an error that I have an undefined reference to pthread_create(). I think I need to type cast something, but what? I already have all the imports I should need.

    Code:
    #include <iostream>
    #include <istream>
    #include <fstream>
    #include <vector>
    #include <sstream>
    #include <pthread.h>
    #include <stdio.h>
    #include <cstdlib>
    
       using namespace std;
    
      
      void *run (void *param);
       
      struct v
      {
      int i;
      int j;
      };
      
      
      
       int main(int argc, char* argv[])
       {
      pthread_t tid; // the thread identifier
       pthread_attr_t attr; // set of thread attributes
       
       pthread_attr_init(&attr);
       
          int size = 0;
          vector<vector<int> > A;
          vector<vector<int> > B;
          
       
       
          fstream fin, fin2;
       
       
       
       if (argc !=3)
       {
       cout << "Houston we have a problem.";
       return 1;
       }
          fin.open(argv[1]);
          fin2.open(argv[2]);
          
       
          int temp = 0;
       
       // reader >> temp;
       
       // A.push_back(temp);
       
       // fin >> temp;
       //A.push_back(temp);
       
       string line;
       string line2;
       
          while (getline(fin, line))
          {
          
             
         istringstream tempStream(line);
    
         vector<int> tempVec;
    
         int temp2;
         while( tempStream >> temp2 )
         {
              tempVec.push_back(temp2);
         }
         if (tempVec.size() !=0)
         {
           A.push_back(tempVec);
           }
          }
        
        
       
          fin.close();
          
            while (getline(fin2, line2))
          {
          
             
         istringstream tempStream(line2);
    
         vector<int> tempVec;
    
         int temp2;
         while( tempStream >> temp2 )
         {
              tempVec.push_back(temp2);
         }
         if (tempVec.size() !=0)
         {
           B.push_back(tempVec);
           }
          }
          fin2.close();
          
       
          for (int i =0; i < A.size(); i++)
          {
             for (int j =0; j < A[i].size(); j++)
             {
                cout << A[i][j]  << " ";
             }
             cout << "\n";
          }
          
          for (int i =0; i < B.size(); i++)
          {
          for (int j = 0; j < B[i].size(); j++)
          {
          cout << B[i][j] << " ";
          }
          cout << "\n";
          }
          
         int M = A.size();
         int N = B[0].size();
         
         int C[M][N];
         
         for (int i =0; i < M; i++)
         {
         
         for (int j =0; j < N; j++)
         {
         struct v *data = (struct v *) malloc(sizeof(struct v));
         data->i = i;
         data->j = j;
         
         pthread_create(&tid, &attr, run, data);
         
         
         }
         
         }
         
       return 0;
       }
       
        void *run(void *param)
       {
       
       
       }

  10. #10

    Re: Not sure how to get started on threading with matrixes in PThreads

    Never mind for that. I think I got it. I told it to use -lpthread after both definitions in the makefile and it worked.

    However, I tried joining threads and it's joining before all of them have even been created, and it's making a segmentation fault.

    Code:
    #include <iostream>
    #include <istream>
    #include <fstream>
    #include <vector>
    #include <sstream>
    #include <pthread.h>
    #include <stdio.h>
    #include <cstdlib>
    
       using namespace std;
    
      
       void *runner (void *param);
       
       struct v
       {
          int i;
          int j;
       };
      
       vector<vector<int> > A;
       vector<vector<int> > B;
      
       int main(int argc, char* argv[])
       {
          pthread_t tid; // the thread identifier
          pthread_attr_t attr; // set of thread attributes
       
          pthread_attr_init(&attr);
       
          int size = 0;
          
          
       
       
          fstream fin, fin2;
       
       
       
          if (argc !=3)
          {
             cout << "Houston we have a problem.";
             return 1;
          }
          fin.open(argv[1]);
          fin2.open(argv[2]);
          
       
          int temp = 0;
       
       // reader >> temp;
       
       // A.push_back(temp);
       
       // fin >> temp;
       //A.push_back(temp);
       
          string line;
          string line2;
       
          while (getline(fin, line))
          {
          
             
             istringstream tempStream(line);
          
             vector<int> tempVec;
          
             int temp2;
             while( tempStream >> temp2 )
             {
                tempVec.push_back(temp2);
             }
             if (tempVec.size() !=0)
             {
                A.push_back(tempVec);
             }
          }
        
        
       
          fin.close();
          
          while (getline(fin2, line2))
          {
          
             
             istringstream tempStream(line2);
          
             vector<int> tempVec;
          
             int temp2;
             while( tempStream >> temp2 )
             {
                tempVec.push_back(temp2);
             }
             if (tempVec.size() !=0)
             {
                B.push_back(tempVec);
             }
          }
          fin2.close();
          
       
          for (int i =0; i < A.size(); i++)
          {
             for (int j =0; j < A[i].size(); j++)
             {
                cout << A[i][j]  << " ";
             }
             cout << "\n";
          }
          
          for (int i =0; i < B.size(); i++)
          {
             for (int j = 0; j < B[i].size(); j++)
             {
                cout << B[i][j] << " ";
             }
             cout << "\n";
          }
          
          int M = A.size();
          int N = B[0].size();
         
          int C[M][N];
         
          for (int i =0; i < M; i++)
          {
          
             for (int j =0; j < N; j++)
             {
                struct v *data = (struct v *) malloc(sizeof(struct v));
                data->i = i;
                data->j = j;
             
             
             
                pthread_create(&tid, &attr, runner, (char *)data);
             
             
             }
          
          
          
          }
         
          pthread_t workers[M][N];
         
          for (int i = 0; i < M; i++)
          {
          
             for (int j =0; j < N; j++)
             {
                cout << "Bang! Bang!  Thread[" << i << "][" << j << "] is dead!";
                pthread_join(workers[i][j], NULL);
             
             
             }
          
          }
         
          return 0;
       }
       
       void *runner(void *param)
       {
       
       
          struct v *fun = (struct v *) param;
       
          int sum = 0;
          for (int n = 0; n < A[0].size(); n++)
          {
             sum = sum + (A[fun->i][n] * B[n][fun->j]);
          
          }
          // C[fun->i][fun->j] = sum;  Hard to find the size of C if you don't know it till the main method.  This was commented out to avoid a compiler error
          cout << "C[" << fun->i << "][" << fun->j << "]=" << sum << "\n";
       
          pthread_exit(0);
       }
    It's kind of working. It's giving me this output:

    1 2 3
    9 5 6
    10 12 15
    1 9 10
    3 5 11
    6 8 12
    C[0][0]=25
    C[0][1]=43
    C[0][2]=68
    C[1][0]=60
    C[1][1]=154
    C[C[2][0]=136
    1][2]=217
    C[2][1]=270
    Bang! Bang! Thread[0][0] is dead!Bang! Bang! Thread[0][1] is dead!C[2][2]=412
    Segmentation fault


    The size is 3 by 3 for both matrices.

    1 2 3
    9 5 6
    10 12 15
    1 9 10
    3 5 11
    6 8 12
    C[0][0]=25
    C[0][1]=43
    C[0][2]=68
    C[1][0]=60
    C[1][1]=154
    C[1][2]=217
    C[2][0]=136
    Bang! Bang! Thread[0][0] is dead!Bang! Bang! Thread[0][1] is dead!C[2][2]=412
    Segmentation fault

    Was another run.
    Last edited by jedipenguin; March 30th, 2012 at 08:40 AM.

  11. #11
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Not sure how to get started on threading with matrixes in PThreads

    This is not valid C++
    Code:
         int M = A.size();
         int N = B[0].size();
         int C[M][N];
    Array sizes can only be determined at compile-time, not run-time. Turn on your ANSI switch for your compiler, and you will see this will not compile.

    You used vector up until now, so why introduce an array? What's wrong with vector<vector<int> >?

    Regards,

    Paul McKenzie

  12. #12
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Not sure how to get started on threading with matrixes in PThreads

    Quote Originally Posted by jedipenguin View Post
    However, I tried joining threads and it's joining before all of them have even been created, and it's making a segmentation fault.
    That's why you have such things as synchronization objects when you create a multithreaded program. Multithreaded programming doesn't just mean "spawning threads". That's why I mentioned earlier to you that multithreaded programming is not "basic" or easy, or can be learned in an ad-hoc way in a few days.

    Where is your usage of synchronization objects? Where is your mutex, semaphore, etc. to signal that the thread has been created?

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center