Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

 Add This Thread To: Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb
 « Previous Thread | Next Thread » Thread Tools Search this Thread Rate Thread Display Modes
 Dev Shed Forums Sponsor:
#1
March 15th, 2013, 05:22 PM
 adamant_387
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 ?

#2
March 15th, 2013, 05:59 PM
 jakotheshadows
Contributing User

Join Date: Aug 2009
Posts: 149
Time spent in forums: 3 Days 12 h 1 m 16 sec
Reputation Power: 35
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.

#3
March 15th, 2013, 08:04 PM
 adamant_387
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

#4
March 15th, 2013, 11:31 PM
 jakotheshadows
Contributing User

Join Date: Aug 2009
Posts: 149
Time spent in forums: 3 Days 12 h 1 m 16 sec
Reputation Power: 35
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.

#5
March 16th, 2013, 03:51 PM
 adamant_387
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.

}
```

#6
March 16th, 2013, 05:04 PM
 salem
Contributed User

Join Date: Jun 2005
Posts: 3,838
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 9 m 51 sec
Reputation Power: 1774
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.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

#7
March 16th, 2013, 05:16 PM
 adamant_387
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;
}
}
}
}```

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Parallel C program

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap