LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-24-2006, 08:17 AM   #1
mozala
LQ Newbie
 
Registered: May 2006
Posts: 8

Rep: Reputation: 0
Threads implementations.


Can somebody help with this code:
Task: Square matrix clockwise rotation using 2*N Threads 1)first quater i.e N/2 (4,7)->(11,6) ..2) second quater (11,16)->(10,13),so on and so forth...
Thanks,
Mozala

Input matrix 4x4

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Matrix 4x4 after rotation:

13 2 3 1
5 10 6 8
9 11 7 12
16 14 15 4

Code:
#include <pthread.h>
#include <iostream>
#define MAX_ROW 10
#define MAX_COL 10
using namespace std;

struct matrix_index_type
{
   int	row;
   int  col;
};

struct matrix_index_type matrix_index[MAX_ROW][MAX_COL];
int aux[MAX_COL];
int matrix[MAX_ROW][MAX_COL];

/*-------------------Read Matrix------------------*/
void read_matrix(int matrix[][MAX_COL]) 
{
    int i,j;     
    for(i = 0; i < MAX_ROW; i ++)
	{
      for(j = 0; j < MAX_COL; j ++)
		{
		  cin<<matrix[i][j];
 
	    }
	}
}

/*--------------PRINT OUT MATRIX-------------------*/
output_matrix(int matrix[][MAX_COL])
{
	int i,j;
	for(i = 0; i < MAX_ROW; i ++)
		{
		  for(j = 0; j < MAX_COL; j ++)
		  {
			  cout<<matrix[i][j]<<endl;
			
		  }
       cout<<"\n";
	}
    cout<<"Finished printing.."<<endl;
}
/*---------------------Rotate---------------------*/
void *rotate( void * index)
{
    int i,j,x;
    struct matrix_index_type *pindex;
    pindex = (struct matrix_index_type *) index;
	i = pindex->row;
	j = pindex->col;

    aux[MAX_COL]=matrix[i][j];
	pthread_mutex_unlock(MD[j])
	   
	   aux[MAX_COL]=matrix[i][j];
	   pthread_mutex_unlock(MD[i]);
	   pthread_mutex_lock(SD[j])
		 
	for (x=0;x<MAX_COL;x++)
	{

        pthread_exit(NULL);
	}
}

	
/*-------------------MAIN PROGRAM-----------------*/
int main()

{

	int Imatrix[][MAX_COL]={1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16};
 	int i, j;
	int index[MAX_ROW][MAX_COL];
	pthread_t ptd[MAX_ROW][MAX_COL];
	
    read_matrix(Imatrix);
	cout<<"Input matrix is:\n"<<endl;
	output_matrix(Imatrix);
	
	for(i = 0; i < MAX_ROW; i ++)
	{
		for(j = 0; j < MAX_COL; j ++)
		{
			matrix_index[i][j].row = i;
			matrix_index[i][j].col = j;
			pthread_create( &ptd[i][j],NULL,rotate, (void*) &matrix_index[i][j]);
		}

	}
	for(i = 0; i < MAX_ROW; i ++)
	{
		for(j = 0; j < MAX_COL; j ++)
		{
			pthread_join( ptd[i][j], NULL);
	        
		}

	}
        cout<<"Rotated matrix is:\n";
	    output_matrix(Imatrix);
} 
       pthread_mutex_destroy(&mutex);
       pthread_exit(NULL);
	   return 0;
  }
 
Old 05-24-2006, 09:06 AM   #2
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
What exactly is your problem? What is MD? You seem to be using a mutex without initializing it, I'm not sure that's allowed. Furthermore, in rotate, you're unlocking a lock before it's locked.
 
Old 05-24-2006, 11:32 AM   #3
mozala
LQ Newbie
 
Registered: May 2006
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by oulevon
What exactly is your problem? What is MD? You seem to be using a mutex without initializing it, I'm not sure that's allowed. Furthermore, in rotate, you're unlocking a lock before it's locked.
Ok ,first thing first
Task: Rotate Main and Secondary diagonal(SD) of the Square matrix clockwise,in other words main will be in the place of secondary and the latter the first place ,so having integers in the matrix my will be runned by 2*N threads meaning i have N*N dimension right? and then to start with the upper half of the main diagonal(MD)will be rotate to the place of the upper half part of SD by the first quarter [1/4(2*N)]of the threads ,the second 1/4 of the threads will take the upper half part of the SD to the lower half part of the main and the third 1/4 of threads will do the same thing so on ,so in which case i have to use global variable and use mutex to avoid race condition right? anyway to cut the long story short....

Rotate the elements of both diagonals in the clock direction.

Input matrix 4x4

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Matrix 4x4 after rotation:

13 2 3 1
5 10 6 8
9 11 7 12
16 14 15 4
Here is a kinda of updated code than the first one..im still tryuing to work on it but i have a problem with rotate it and then at same time taking care of mutex and its kinda confusing even i feel like im close to the end...thats why i need help..my head banging.,.

Code:
#include <pthread.h>
#include <iostream>
#define M    10
#define N    10
using namespace std;

struct matrix_index_type
{
   int	row;
   int  col;
};

struct matrix_index_type matrix_index[M][N];
int aux[N];
pthread_mutex_t SD[j];
pthread_mutex_t MD[i];
/*-------------------Read Matrix------------------*/
void read_matrix(int matrix[][N]) 
{
    int i,j;     
    for(i = 0; i < M; i ++)
	{
      for(j = 0; j < M; j ++)
		{
		  cin<<matrix[i][j];
 
	    }
	}
}

/*--------------PRINT OUT MATRIX-------------------*/
output_matrix(int matrix[][N])
{
	int i,j;
	for(i = 0; i < M; i ++)
		{
		  for(j = 0; j < N; j ++)
		  {
			  cout<<matrix[i][j]<<endl;
			
		  }
       cout<<"\n";
	}
    cout<<"Finished printing.."<<endl;
}
/*---------------------Rotate---------------------*/
void *rotate( void * index)
{
    int i,j,x;
    struct matrix_index_type *pindex;
    pindex = (struct matrix_index_type *) index;
	i = pindex->row;
	j = pindex->col;


	for(i = 0; i < N-1; i++)
	
	{
	  for(j = 0; j <N-1; j++)
		{
    pthread_mutex_lock(MD[i]);
    aux[N]=matrix[i][j];
	pthread_mutex_unlock(MD[i]);	   
	pthread_mutex_lock(SD[j]);
    
		if(i < N/2) && (j<N/2)
		{
           matrix[i][j+N/2]=aux[N];
		}

        if(i > N/2) && (j > N/2)
		{
			
           matrix[i][j+N/2]=aux[N];

		}

		}
	}

	for(i = 0; i < N-1; i++)
	
	{
		for(j = N-1; j > 0; j--)
		{
	pthread_mutex_lock(SD[j])	      
    aux[N]=matrix[i][j];
	pthread_mutex_unlock(SD[j]);	   
	pthread_mutex_lock(MD[i]);
    	
      if(i < N/2) && (j > N/2) 

		{
           matrix[i][j+N/2]=aux[N];
		}

        if(i > N/2) && (j < N/2)
		{
			
           matrix[i][j+N/2]=aux[N];

		}


		}
	}

	
    
	for (x=0;x<MAX_COL;x++)
	{

        pthread_exit(NULL);
	}
}
	
/*-------------------MAIN PROGRAM-----------------*/
int main()

{

	int Imatrix[][N]={1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16};
 	int i, j;
	int index[M][N];
	pthread_t ptd[M][N];
	
    read_matrix(Imatrix);
	cout<<"Input matrix before being been rotated is:\n"<<endl;
	output_matrix(Imatrix);
	
	for(i = 0; i < N; i ++)
	{
		for(j = 0; j < N; j ++)
		{
			matrix_index[i][j].row = i;
			matrix_index[i][j].col = j;
			pthread_create( &ptd[i][j],NULL,rotate, (void*) &matrix_index[i][j]);
		}

	}
	for(i = 0; i < M; i ++)
	{
		for(j = 0; j < N; j ++)
		{
			pthread_join( ptd[i][j], NULL);
	        
		}

	}
        cout<<"Rotated matrix is:\n";
	    output_matrix(Imatrix);
} 
       pthread_mutex_destroy(&mutex);
       pthread_exit(NULL);
	   return 0;
  }
 
Old 05-24-2006, 12:11 PM   #4
mozala
LQ Newbie
 
Registered: May 2006
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by oulevon
What exactly is your problem? What is MD? You seem to be using a mutex without initializing it, I'm not sure that's allowed. Furthermore, in rotate, you're unlocking a lock before it's locked.

Ok ,first thing first
Task: Rotate Main and Secondary diagonal(SD) of the Square matrix clockwise,in other words main will be in the place of secondary and the latter the first place ,so having integers in the matrix my will be runned by 2*N threads meaning i have N*N dimension right? and then to start with the upper half of the main diagonal(MD)will be rotate to the place of the upper half part of SD by the first quarter [1/4(2*N)]of the threads ,the second 1/4 of the threads will take the upper half part of the SD to the lower half part of the main and the third 1/4 of threads will do the same thing so on ,so in which case i have to use global variable and use mutex to avoid race condition right? anyway to cut the long story short....

Rotate the elements of both diagonals in the clock direction.

Input matrix 4x4

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Matrix 4x4 after rotation:

13 2 3 1
5 10 6 8
9 11 7 12
16 14 15 4
Here is a kinda of updated code than the first one..im still tryuing to work on it but i have a problem with rotate it and then at same time taking care of mutex and its kinda confusing even i feel like im close to the end...thats why i need help..my head banging.,.



Code:
#include <pthread.h>
#include <iostream>
#define M    10
#define N    10
using namespace std;

struct matrix_index_type
{
   int	row;
   int  col;
};

struct matrix_index_type matrix_index[M][N];
int aux[N];

/*-------------------Read Matrix------------------*/
void read_matrix(int matrix[][N]) 
{
    int i,j;     
    for(i = 0; i < M; i ++)
	{
      for(j = 0; j < M; j ++)
		{
		  cin<<matrix[i][j];
 
	    }
	}
}

/*--------------PRINT OUT MATRIX-------------------*/
output_matrix(int matrix[][N])
{
	int i,j;
	for(i = 0; i < M; i ++)
		{
		  for(j = 0; j < N; j ++)
		  {
			  cout<<matrix[i][j]<<endl;
			
		  }
       cout<<"\n";
	}
    cout<<"Finished printing.."<<endl;
}
/*---------------------Rotate---------------------*/
void *rotate( void * index)
{
    int i,j,x;
    struct matrix_index_type *pindex;
    pindex = (struct matrix_index_type *) index;
	i = pindex->row;
	j = pindex->col;


	for(i = 0; i < N-1; i++)
	
	{
	  for(j = 0; j <N-1; j++)
		{
    pthread_mutex_lock(MD[i]);
    aux[N]=matrix[i][j];
	pthread_mutex_unlock(MD[i]);	   
	pthread_mutex_lock(SD[j]);
    
		if(i < N/2) && (j<N/2)
		{
           matrix[i][j+N/2]=aux[N];
		}

        if(i > N/2) && (j > N/2)
		{
			
           matrix[i][j+N/2]=aux[N];

		}

		}
	}

	for(i = 0; i < N-1; i++)
	
	{
		for(j = N-1; j > 0; j--)
		{
	pthread_mutex_lock(SD[j])	      
    aux[N]=matrix[i][j];
	pthread_mutex_unlock(SD[j]);	   
	pthread_mutex_lock(MD[i]);
    	
      if(i < N/2) && (j > N/2) 

		{
           matrix[i][j+N/2]=aux[N];
		}

        if(i > N/2) && (j < N/2)
		{
			
           matrix[i][j+N/2]=aux[N];

		}


		}
	}

	
    
	for (x=0;x<MAX_COL;x++)
	{

        pthread_exit(NULL);
	}
}
	
/*-------------------MAIN PROGRAM-----------------*/
int main()

{

	int Imatrix[][N]={1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16};
 	int i, j;
	int index[M][N];
	pthread_t ptd[M][N];
    pthread_mutex_t SD[j];
    pthread_mutex_t MD[i];
	
    read_matrix(Imatrix);
	cout<<"Input matrix before being been rotated is:\n"<<endl;
	output_matrix(Imatrix);
	

/*Initialize mutexes  */
       for (int i = 0; i < N; i++)
	   {
               pthread_mutex_init(&MD[i], NULL);
	   }
        for (j = 0; j < N; j++)
		{
			   pthread_mutex_init(&SD[j], NULL);
		}
/* Creating Threads  */
	for(i = 0; i < N; i ++)
	{
		for(j = 0; j < N; j ++)
		{
			matrix_index[i][j].row = i;
			matrix_index[i][j].col = j;
			pthread_create( &ptd[i][j],NULL,rotate, (void*) &matrix_index[i][j]);
		}

	}
	/*Waiting for all threads completion*/
	for(i = 0; i < M; i ++)
	{
		for(j = 0; j < N; j ++)
		{
			pthread_join( ptd[i][j], NULL);
	        
		}

	}
        cout<<"Rotated matrix is:\n";
	    output_matrix(Imatrix);
} 
      pthread_mutex_destroy(&SD[j]);
      pthread_mutex_destroy(&MD[j]);
      pthread_exit(NULL);
	   return 0;
  }
 
Old 05-24-2006, 12:19 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Sure sounds like a homework assignment to me...
 
Old 05-24-2006, 01:38 PM   #6
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by sundialsvcs
Sure sounds like a homework assignment to me...
Me too.
change the extraction to the insertion operator in read.
Does the programme hang?
Check your locks and unlocks in rotate.

Is this valid?
Code:
...
      if(i < N/2) && (j > N/2) 

		{
           matrix[i][j+N/2]=aux[N];
		}

        if(i > N/2) && (j < N/2)
...

Last edited by dmail; 05-24-2006 at 01:41 PM.
 
Old 05-24-2006, 03:22 PM   #7
mozala
LQ Newbie
 
Registered: May 2006
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by sundialsvcs
Sure sounds like a homework assignment to me...
U r comedian indeed

Even if it was ,this forum is to help one with the code that has already been written ,right?But strictly not to write the whole code for ppl right?So whats ur score ?U have no idea how to help ,then u better save ur breath ,For God sake!!,for easier ones...
 
Old 05-24-2006, 04:32 PM   #8
mozala
LQ Newbie
 
Registered: May 2006
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by dmail
Me too.
change the extraction to the insertion operator in read.
Does the programme hang?
Check your locks and unlocks in rotate.

Is this valid?
Code:
...
      if(i < N/2) && (j > N/2) 

		{
           matrix[i][j+N/2]=aux[N];
		}

        if(i > N/2) && (j < N/2)
...
If its not ,have u any better idea?
Quote:
Check your locks and unlocks in rotate.
 
Old 05-24-2006, 04:45 PM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I've not looked at your code but you might find it easier to tackle it in two steps:
1) Solve it with a standard function that performs the rotations
2) Then put each invokation of the rotate function into its own thread.

This might make it easier to isolate the problem.
 
Old 05-24-2006, 05:06 PM   #10
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
Quote:
Originally Posted by mozala
If its not ,have u any better idea?
I think his point is that your code isn't valid syntax. This is valid:

Code:
     if((i < N/2) && (j > N/2)) 
     {
           matrix[i][j+N/2]=aux[N];
      }

      if((i > N/2) && (j < N/2)){
      }
 
Old 05-24-2006, 05:09 PM   #11
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
beat me to it oulevon

Quote:
Originally Posted by mozala
If its not ,have u any better idea?
Code:
...
      if(i < N>>1 && j > N>>1)  

		{
           matrix[i][j+N/2]=aux[N];
		}

        if(i > N>>1 && j < N>>1) 
...
or without bitshifts
Code:
...
      if(i < N/2 && j > N/2) 

		{
           matrix[i][j+N/2]=aux[N];
		}

        if(i > N/2 && j < N/2)
...
Its the placing of the parentheses that I am questioning and wether the if condition is valid.
ie if(something) && (something)
does this evaluate to
if( something && something)??


[edit]
better still cache N/2.
Code:
{
//cache the int value of N/2 within the scope of the braces
int half_n = N/2;
     for(i = 0; i < N-1; i++)
     {
          for(j = 0; j <N-1; j++)
          {
               pthread_mutex_lock(MD[i]);
               aux[N]=matrix[i][j];
               pthread_mutex_unlock(MD[i]);	   
               pthread_mutex_lock(SD[j]);
    
               if(i < half_n && j<half_n)
               {
                    matrix[i][j+half_n]=aux[N];
               }

               if(i > half_n && j > half_n)
               {		
                    matrix[i][j+half_n]=aux[N];

               }

          }
     }
}//scope of N/2 ends
[editedit]
Quote:
U r comedian indeed

Even if it was ,this forum is to help one with the code that has already been written ,right?But strictly not to write the whole code for ppl right?So whats ur score ?U have no idea how to help ,then u better save ur breath ,For God sake!!,for easier ones...
Comments like the above neither help the forum or make people want to help you
Quote:
...U have no idea how to help
forgive me if I am wrong but from what I see you have dumped code and not explained what your problem is!

Quote:
Originally Posted by http://lordandmaker.co.uk/forumhelp/forumhelp.htm
...
- Body

This should be easy to read and in proper English. Re-read it when you've finished, to make sure it makes sense. Cut out any background information that isn't necessary, but put in all the useful information you have. If you're not sure whether something is useful or not, put it at the bottom.

Say what went wrong, and when. Describe what you've tried, and the results of that.

Describe the symptoms of the problem as accurately as possible, and don't make guesses as to what might be causing the problem. The closer the information you give the forum is to the raw information you see, the more likely you are to get a relevant answer

Ask accurate questions. Asking "i need some information about ______" will not get you a useful answer. You need to specify what information you want and, if necessary, why. If your question can be answered with a 'yes' or 'no', you will likely get that answer.
...

Last edited by dmail; 05-24-2006 at 05:23 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java threads listed using kill -3 does not contain all threads found using ps -auxww coneheed Programming 2 11-14-2005 08:57 AM
UFS-Implementations spinifeX *BSD 1 01-09-2005 12:01 AM
IPSec / all implementations telmich Linux - Software 1 07-15-2003 08:17 AM
TCP implementations sabeel_ansari Linux - Networking 3 06-10-2002 01:54 PM
interesting LFS implementations? trub Linux From Scratch 3 02-14-2002 08:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration