The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Parallel C program
Discuss Parallel C program in the C Programming forum on Dev Shed. Parallel C program C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 15th, 2013, 05:22 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 4
Time spent in forums: 1 h 6 m 53 sec
Reputation Power: 0
|
|
|
Parallel C program
I'm making a parallel C program of matix mult with multiple processes . I am having difficulty in assigning various rows to different processes to compute . Can anyone help me with this ?
|

March 15th, 2013, 05:59 PM
|
 |
Contributing User
|
|
|
|
|
There are many ways to gut this fish, so the first thing you should look at are the constraints and specifications your instructor gives you. You might have each child process compute a row or compute a column of the output matrix. Suppose each child computes a row of the output. You could then provide each child process with a row to compute and both input matrices. However, this may not be the way your professor wants you to do this.
|

March 15th, 2013, 08:04 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 4
Time spent in forums: 1 h 6 m 53 sec
Reputation Power: 0
|
|
Suppose I have to create not more than 6 and then compute 20*20 matrix. That means one child process will compute more than one rows. Either I can make one process do more than one rows at a time and run them in parallel or I can make one row per process, making 6 rows compute at a time then doing sequentially and reusing those processes to do next 6. But I am baffled to construct it. Thanks 
|

March 15th, 2013, 11:31 PM
|
 |
Contributing User
|
|
|
|
Quote: | Originally Posted by adamant_387 Either I can make one process do more than one rows at a time and run them in parallel or I can make one row per process, making 6 rows compute at a time then doing sequentially and reusing those processes to do next 6 |
That second option doesn't even make sense. If you're limited to N processes just fairly divide up your M output rows among your N processes such that each process has at least M / N rows to compute, or if M is less than N just use M processes.
Beyond this you need to show some code that demonstrates an attempt to implement this design.
|

March 16th, 2013, 03:51 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 4
Time spent in forums: 1 h 6 m 53 sec
Reputation Power: 0
|
|
Thanks , I know second one is feasible but I am unable to program it . For example , I have 11 rows M%N = 1 what will happen to this remaining row . Which process will take care of it. I am [trying to write code for it . It is not complete but shows intent of what I am trying to do . Please suggest some modifications.
Code:
#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define MAX_PROC 10
struct mul
{
float **first
float **second
float **result
int m,n,p,q,row;
}
int main()
{
int i,j,k;
int pid;
int shmid1,shmid2,shmid3,shmid4; // shared memory handler
key_t key1,key2,key3; //key - name for sm(shared memory) area.
char *shmfirst,*shmsecond,*shmresult,*shmstruct;
key1 = 123,key2=4644,key3=98883,key4=3874
if ((shmid1 = shmget(key1,sizeof(struct mul), 0666)) < 0) //create the sm area for matrix first
{
perror("shmget");
exit(1);
}
if ((shmstruct = shmat(shmid1, NULL, 0)) == (char *) -1) // attach it to the local address space and get the pointer to it: shmmul
{
perror("shmat errorneous");
exit(1);
}
shmmul=(struct mul *)shmstruct; // struct object
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &shmmul->m, &shmmul->n);
if ((shmid2 = shmget(key1,sizeof(first[shmmul->m][shmmul->n]), 0666)) < 0) //create the sm area for matrix first
{
perror("shmget");
exit(1);
}
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &shmmul->p, &shmmul->q);
if ((shmid3 = shmget(key2,sizeof(second[shmmul->p][shmmul->q]), 0666)) < 0) //create the sm area for matrix second
{
perror("shmget");
exit(1);
}
if ((shmid4 = shmget(key3,sizeof(result[shmmul->m][shmul->q]), 0666)) < 0) //create the sm area for result matrix
{
perror("shmget");
exit(1);
}
if ((shmfirst = shmat(shmid2, NULL, 0)) == (char *) -1) // attach it to the local address space and get the pointer to it: first
{
perror("shmat errorneous");
exit(1);
} if ((shmsecond = shmat(shmid3, NULL, 0)) == (char *) -1) // attach it to the local address space and get the pointer to it: second
{
perror("shmat errorneous");
exit(1);
} if ((shmresult = shmat(shmid4, NULL, 0)) == (char *) -1) // attach it to the local address space and get the pointer to it: third
{
perror("shmat errorneous");
exit(1);
}
shmmul->first= ((float*))shmfirst;
shmmul->second= ((float*))shmsecond;
shmmul->result= ((float*))shmresult;
printf("Enter the elements of A matrix\n");
for( i = 0 ; i <shmmul-> m ; i++ )
for ( j = 0 ; j < shmmul->n ; j++ )
scanf("%f", &(shmmul->first[i][j]));
printf("Enter the elements of A matrix\n");
for( i = 0 ; i <shmmul-> p ; i++ )
for ( j = 0 ; j < shmmul->q; j++ )
scanf("%f", &(shmmul->second[i][j]));
{
for(i=0;i<shmmul->m;i++)
{
if (shmmul->m<MAX_PROC)
fork();
else
for(i=0;i<MAX_PROC;i++)
{
pid=fork();
}
if(pid=0)
{
int x,j=0;
for(x=0;x<shmmul->q;x++)
{
sum=0.00;
int shmmul->row=i;
for(j=0;j<;j++)
{
printf("%f %f %f \n", shmmul->first[shmmul->row][j], second[j][x],
first[shmmul->row][j] * second[j][x]);
sum += first[shmmul->row][j] * second[j][x]);
}
[shmmul->row][x] = sum;
}
}
}
}
//destruction of the sm area:
shmdt(shmstruct); //detach the sm
shmctl(shmid1, IPC_RMID, 0); //destroy it.
shmdt(shmfirst); //detach the sm
shmctl(shmid2, IPC_RMID, 0); //destroy it.
shmdt(shmsecond); //detach the sm
shmctl(shmid3, IPC_RMID, 0); //destroy it.
shmdt(shmresult); //detach the sm
shmctl(shmid4, IPC_RMID, 0); //destroy it.
}
|

March 16th, 2013, 05:04 PM
|
 |
Contributed User
|
|
|
|
IMO, you shouldn't be writing code using fork() until you've learnt how to indent code.
Multi-threaded (or multi-process) code is hard enough to do when the code is nice and clean, but when it looks like dog-food to start with, then forget it.
Code:
#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define MAX_PROC 10
struct mul {
float **first;
float **second;
float **result;
int m, n, p, q, row;
};
int main()
{
int i, j, k;
int pid;
int shmid1, shmid2, shmid3, shmid4; // shared memory handler
key_t key1, key2, key3; //key - name for sm(shared memory) area.
char *shmfirst, *shmsecond, *shmresult, *shmstruct;
key1 = 123, key2 = 4644, key3 = 98883, key4 = 3874;
if ((shmid1 = shmget(key1, sizeof(struct mul), 0666)) < 0) //create the sm area for matrix first
{
perror("shmget");
exit(1);
}
if ((shmstruct = shmat(shmid1, NULL, 0)) == (char *) -1) // attach it to the local address space and get the pointer to it: shmmul
{
perror("shmat errorneous");
exit(1);
}
shmmul = (struct mul *) shmstruct; // struct object
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &shmmul->m, &shmmul->n);
if ((shmid2 = shmget(key1, sizeof(first[shmmul->m][shmmul->n]), 0666)) < 0) //create the sm area for matrix first
{
perror("shmget");
exit(1);
}
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &shmmul->p, &shmmul->q);
if ((shmid3 = shmget(key2, sizeof(second[shmmul->p][shmmul->q]), 0666)) < 0) //create the sm area for matrix second
{
perror("shmget");
exit(1);
}
if ((shmid4 = shmget(key3, sizeof(result[shmmul->m][shmul->q]), 0666)) < 0) //create the sm area for result matrix
{
perror("shmget");
exit(1);
}
if ((shmfirst = shmat(shmid2, NULL, 0)) == (char *) -1) // attach it to the local address space and get the pointer to it: first
{
perror("shmat errorneous");
exit(1);
}
if ((shmsecond = shmat(shmid3, NULL, 0)) == (char *) -1) // attach it to the local address space and get the pointer to it: second
{
perror("shmat errorneous");
exit(1);
}
if ((shmresult = shmat(shmid4, NULL, 0)) == (char *) -1) // attach it to the local address space and get the pointer to it: third
{
perror("shmat errorneous");
exit(1);
}
shmmul->first = ((float *)) shmfirst;
shmmul->second = ((float *)) shmsecond;
shmmul->result = ((float *)) shmresult;
printf("Enter the elements of A matrix\n");
for (i = 0; i < shmmul->m; i++)
for (j = 0; j < shmmul->n; j++)
scanf("%f", &(shmmul->first[i][j]));
printf("Enter the elements of A matrix\n");
for (i = 0; i < shmmul->p; i++)
for (j = 0; j < shmmul->q; j++)
scanf("%f", &(shmmul->second[i][j]));
{
for (i = 0; i < shmmul->m; i++) {
if (shmmul->m < MAX_PROC)
fork();
else
for (i = 0; i < MAX_PROC; i++) {
pid = fork();
}
if (pid = 0) {
int x, j = 0;
for (x = 0; x < shmmul->q; x++) {
sum = 0.00;
int shmmul->row = i;
for (j = 0; j <; j++) {
printf("%f %f %f \n",
shmmul->first[shmmul->row][j], second[j][x],
first[shmmul->row][j] * second[j][x]);
sum += first[shmmul->row][j] * second[j][x];
}
[shmmul->row][x] = sum;
}
}
}
}
//destruction of the sm area:
shmdt(shmstruct); //detach the sm
shmctl(shmid1, IPC_RMID, 0); //destroy it.
shmdt(shmfirst); //detach the sm
shmctl(shmid2, IPC_RMID, 0); //destroy it.
shmdt(shmsecond); //detach the sm
shmctl(shmid3, IPC_RMID, 0); //destroy it.
shmdt(shmresult); //detach the sm
shmctl(shmid4, IPC_RMID, 0); //destroy it.
}
Second, if you're going to post code, then at least make sure it COMPILES, unless your question is about compiler error messages.
Code:
$ gcc -std=c99 -Wall -D_SVID_SOURCE foo.c
foo.c: In function ‘main’:
foo.c:22:42: error: ‘key4’ undeclared (first use in this function)
foo.c:22:42: note: each undeclared identifier is reported only once for each function it appears in
foo.c:26:5: warning: implicit declaration of function ‘exit’ [-Wimplicit-function-declaration]
foo.c:26:5: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
foo.c:32:5: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
foo.c:34:3: error: ‘shmmul’ undeclared (first use in this function)
foo.c:38:37: error: ‘first’ undeclared (first use in this function)
foo.c:41:5: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
foo.c:46:37: error: ‘second’ undeclared (first use in this function)
foo.c:49:5: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
foo.c:52:37: error: ‘result’ undeclared (first use in this function)
foo.c:52:55: error: ‘shmul’ undeclared (first use in this function)
foo.c:55:5: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
foo.c:61:5: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
foo.c:67:5: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
foo.c:73:5: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
foo.c:76:29: error: expected expression before ‘)’ token
foo.c:77:30: error: expected expression before ‘)’ token
foo.c:78:30: error: expected expression before ‘)’ token
foo.c:98:7: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
foo.c:101:11: error: ‘sum’ undeclared (first use in this function)
foo.c:102:21: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token
foo.c:102:21: error: expected expression before ‘->’ token
foo.c:104:26: error: expected expression before ‘;’ token
foo.c:110:11: error: expected expression before ‘[’ token
foo.c:17:13: warning: unused variable ‘k’ [-Wunused-variable]
There's nothing we can do to fix up this amount of problems, and have anything resembling what you want it to be.
I changed a couple of things, but that was only to get indent to accept the program to format it.
Third, I would suggest you start with a known working single-threaded program which does what you want without all the complication of processes and shared memory.
In particular, put code into functions, so you don't have 100+ line of main() trying to do everything.
So instead of this in main
Code:
for ( ... ) {
for ( ... ) {
}
}
You have
Code:
void calculate ( int i ) {
for ( ... ) {
for ( ... ) {
}
}
}
Which gets called from main as
Code:
for ( i = 0 ; i < 10 ; i++ ) {
calculate(i);
}
So that when you come to make it multi-process, it becomes
Code:
for ( i = 0 ; i < 10 ; i++ ) {
if ( fork() == 0 ) {
calculate(i);
}
}
If you think carefully about the interfaces you're going to need, you can smooth the way from single-process to multi-process with very little effort.
|

March 16th, 2013, 05:16 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 4
Time spent in forums: 1 h 6 m 53 sec
Reputation Power: 0
|
|
OH sorry about that , I will do the required changed and post it again ! That was a mess , I admit. I have compiled it though. I am trying to improve it. I don't know if I am going in right direction. I am getting an error:warning: incompatible implicit declaration of built-in function exit
Code:
#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define MAX_PROC 10
struct mul
{
float **first;
float **second;
float **result;
int m,n,p,q,row;
};
int main()
{
int i,j,k;
int pid;
int shmid1,shmid2,shmid3,shmid4; // shared memory handler
key_t key1,key2,key3,key4; //key - name for sm(shared memory) area.
char *shmfirst,*shmsecond,*shmresult,*shmstruct;
key1 = 123,key2=4644,key3=98883,key4=3874;
if ((shmid1 = shmget(key1,sizeof(struct mul), 0666)) < 0) //create the sm area for matrix first
{
perror("shmget");
exit(1);
}
if ((shmstruct = shmat(shmid1, NULL, 0)) == (char *) -1) // attach it to the local address space and get the pointer to it: shmmul
{
perror("shmat errorneous");
exit(1);
}
struct mul *shmmul=(struct mul *)shmstruct; // struct object
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &shmmul->m, &shmmul->n);
if ((shmid2 = shmget(key1,sizeof(shmmul->first[shmmul->m][shmmul->n]), 0666)) < 0) //create the sm area for matrix first
{
perror("shmget");
exit(1);
}
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &shmmul->p, &shmmul->q);
if ((shmid3 = shmget(key2,sizeof(shmmul->second[shmmul->p][shmmul->q]), 0666)) < 0) //create the sm area for matrix second
{
perror("shmget");
exit(1);
}
if ((shmid4 = shmget(key3,sizeof(shmmul->result[shmmul->m][shmmul->q]), 0666)) < 0) //create the sm area for result matrix
{
perror("shmget");
exit(1);
}
if ((shmfirst = shmat(shmid2, NULL, 0)) == (char *) -1) // attach it to the local address space and get the pointer to it: first
{
perror("shmat errorneous");
exit(1);
} if ((shmsecond = shmat(shmid3, NULL, 0)) == (char *) -1) // attach it to the local address space and get the pointer to it: second
{
perror("shmat errorneous");
exit(1);
} if ((shmresult = shmat(shmid4, NULL, 0)) == (char *) -1) // attach it to the local address space and get the pointer to it: third
{
perror("shmat errorneous");
exit(1);
}
shmmul->first= (float**)shmfirst;
shmmul->second= (float**)shmsecond;
shmmul->result= (float**)shmresult;
printf("Enter the elements of first matrix\n");
for( i = 0 ; i <shmmul-> m ; i++ )
for ( j = 0 ; j < shmmul->n ; j++ )
scanf("%f", &(shmmul->first[i][j]));
printf("Enter the elements of second matrix\n");
for( i = 0 ; i <shmmul-> p ; i++ )
for ( j = 0 ; j < shmmul->q; j++ )
scanf("%f", &(shmmul->second[i][j]));
{
for(i=0;i<shmmul->m;i++)
{
if (shmmul->m<MAX_PROC)
fork();
else
for(i=0;i<MAX_PROC;i++)
{
pid=fork();
}
if(pid == 0)
{
int x,j=0;
for(x=0;x<shmmul->q;x++)
{
int sum;
sum=0.00;
shmmul->row=i;
for(j=0;j<q;j++)
{
printf("%f %f %f \n", shmmul->first[shmmul->row][j], shmmul->second[j][x],
shmmul->first[shmmul->row][j] * shmmul->second[j][x]);
sum += shmmul->first[shmmul->row][j] * shmmul->second[j][x];
}
shmmul->result[shmmul->row][x] = sum;
}
}
}
}
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|