Solved

Problem in passing arguments between files in c

Posted on 2012-03-29
23
243 Views
Last Modified: 2012-03-29
Hi all,
I'm having the problem in passing the num_thread from my main.c to mcompute.c, in line 39 it prints out 0(instead of 2 from the command line argument). hence, i got an Arithmetic Exception (core dumped) after it.
Can anyone help me in this, my code is a little messy since this is my first time splitting up the code into several files, so i'm kind of weak in passing the arguments.

These are the global var i have in my matmul.h:
int num_thread;      // number of threads
int      itt_max;      // number of itterations to preform
int      seed;            // seed for srand48() / drand48()
int      n;                  // problenm size
double      *a;            // pointer to transfromation array, space to be malloced
double      *b;            // pointer to transfromation vector, space to be malloced
double *err;

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include "matmul.h"

void srand48(long int seedval);
double drand48(void);

void* compute(void* thrd)
{	
	int from, to;
	int s = (int)thrd;
	int i, j;
	double  *t;     // pointer to solution vector, space to be malloced 
	double  *t1;    // pointer to next itteraton of solution vector, 
	                // space to be malloced
	double	*ttemp;	// used to swap t1 and t at each itteration
	int	itt;	// current itteration 
	double	sum;	// computes the inner products for A * t
	double 	error;  // max | t1[i] - t[i] |
	double 	errori; // | t1[i] - t[i] |
	
	if( (t=(double *)malloc(sizeof(double)*n)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for t failed\n");
	}
	
	if( (t1=(double *)malloc(sizeof(double)*n)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for t1 failed\n");
	}
	
	if( (err = (double*) malloc (sizeof (double) * itt_max)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for err failed\n");
	}	
	
	printf("Number of threads is: %d \n\n", num_thread);
	for(i=0; i< n; i++) 
	{
  		t[i] = b[i];
	}
	
	if( s < num_thread)
	{
		from = (s * (itt_max + 1))/num_thread;
		to = ((s + 1) * (itt_max + 1))/num_thread - 1;
	}
	else if ( s == num_thread)
	{
		from = (s * (itt_max + 1))/num_thread;
		to = ((s + 1) * (itt_max + 1))/num_thread;
	}

	for(itt=from; itt<=to; itt++) 
	{
  		error=0.0;
  		for(i=0; i< n; i++) 
		{
    		sum = 0.0;
			double *a_itt = a + n*i;
			for(j=0; j< n; j++) 
			{
			   sum += *(a_itt++) * t[j];
			}
    		t1[i] = sum + b[i];
    		errori = fabs(t1[i]-t[i]);
    		if(errori > error) 
			{
      			error=errori;
    		}
  		}
		err[itt] = error;
  		ttemp = t1;
  		t1 = t;
  		t = ttemp;
  		//printf("%5d %14.6e\n", itt, error); 
	}

	pthread_exit(0);
}

int mcompute(int n, int seed, int itt_max, int num_thread, double *a, double *b)
{
	srand48((long int)seed);
	float resultb[n];	
	int	i, j;   // indices into arrays
	pthread_t* thread;	// pointer to a group of threads

	float **resulta = (float**)malloc(sizeof(float*)*n);
	for(i=0; i< n; i++)
		*(resulta+i) = (float*)malloc(sizeof(float)*n);

	for(i=0; i< n; i++) 
	{
  		for(j=0; j< n; j++) 
		{
    			*(a+n*i+j) = 1.999 * (drand48() - 0.5) / n;
    			resulta[i][j] = *(a+n*i+j);
  		}
	}

	moutput_a(resulta, n);

	// Generate vector b 
	for(i=0; i< n; i++) 
	{
  		b[i] = 10.0 * drand48();
		resultb[i] = b[i];
	}

	moutput_b(resultb, n);
	
	thread = (pthread_t*)malloc(num_thread*sizeof(pthread_t));
	
	for (i = 0; i < num_thread; i++)
	{
		if(pthread_create(&thread[i], NULL, compute, (void*)i) != 0)
		{
			perror("Can't create thread!");
			free(thread);
			exit(-1);
		}
	}
	
	for (i = 0; i < num_thread; i++)
		pthread_join (thread[i], NULL);

	moutput_itt(err, itt_max);
	
	free(thread);
	return 0;
}

Open in new window

0
Comment
Question by:crazy4s
  • 12
  • 10
23 Comments
 
LVL 24

Expert Comment

by:fridom
ID: 37781661
num_thread is never set. And you do not show the code where it is. So whatever the outpuut is, it is ok.
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 37781666
Hi crazy4s,

IMO you somehow mix local and global use of the variable num_threads, but it's hard to say how you expect it to work since you didn't post the code of the mentioned main.

What I can tell you is that compute uses a global declared num_threads, mcompute instead uses a local declare num_threads passed as argument.

Without seing the call of this function I can't tell you more, sorry.

ZOPPO
0
 

Author Comment

by:crazy4s
ID: 37781688
0
 
LVL 30

Accepted Solution

by:
Zoppo earned 500 total points
ID: 37781840
ok, first I would suggest to rename the global variable to make it easier to distinguish between the global and the local used variables.

Next I'm a bit suprised why this doesn't produce any linker errors, but maybe your compiler/linker acts different than those I use. Generally a problem is that you instantiate the global num_threads within the header, so in each c-file where you include mathmul.h a new instance is created. To get only one global instance you'll have to declare it as extern in the header and instantiate it in exactly one c-file.

So you can do it somehow like this:
// mathmul.h
...
extern int global_thread_num;
...

Open in new window

// mathmul-compute.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include "matmul.h"

int global_thread_num = 0;
...
void* compute(void* thrd)
{
 // replace all occurances of thread_num with global_thread_num in this function
}

int mcompute(int n, int seed, int itt_max, int num_thread, double *a, double *b)
{
 global_num_thread = num_thread;
 // you can replace all further occurances of thread_num with global_thread_num in this function to avoid confusion, but it's not necessary
 ...
} 

Open in new window


BTW, you should change the instantiation in the header for the other global variables in mathmul.h the same way (declare as extern in the header, instantiate once in a c-file).

Hope that helps,

ZOPPO
0
 

Author Comment

by:crazy4s
ID: 37781881
and how bout those with pointer do i do the same too:
double      *a;

do i need to change it to extern double *global_a ???

and since i'm accessing the global variable, do i actually still need to pass those variables?
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 37781975
For every global thing you need/want to use in multiple C-files you have to declare it as extern in all of these files (usually by declaring it in a header which is include in each of these files), no matter if variables, pointers (which are variables too) or functions. You can even mix where you instantiate/implement them and you can even do it if wanted in single C-files only

For example:

// header.h
// (declaration in header allows use in every c-file which includes the header)
extern int global_int;
extern double* global_ptr;
extern int global_function( int value );

// cfile1.c
#include "header.h"

int global_int = 0; // instantiation
double local_double = 0.0; // instantiation of a local variable

int global_function( int value ); // implementation
{
 // global pointer accessed here allthough it's not instantiated in this file
 global_ptr = malloc( sizeof( double ) );
}

// cfile2.c
#include "header.h"

extern double local_double; // this tells the compilet/linker the variable exists but is instantiated elsewhere, so you can use it here
double* global_ptr = &local_double; // instantiation;

Open in new window


Hope that's helpful.

And, for the second question: If you simply use global_num_threads with the scanf in your main function you can use it everywhere else you used num_thread before without the need to pass it anywhere else.

ZOPPO
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 37782002
0
 

Author Comment

by:crazy4s
ID: 37782038
between is it possible to pass those variables without using global var?
this means locally pass from main to setup, compute, and output.
which is better?
0
 
LVL 30

Assisted Solution

by:Zoppo
Zoppo earned 500 total points
ID: 37782127
It is usually possible to simply pass the variable as parameters to any function where you need, but there are some cases where it's not simply possible because a function has to be declared with a given signature - that's the case in your code because you cannot change the paramter list of compute to pass two variables since it's passed to pthread_create and therefore cannot be declared to get two parameters passed.

But you can implement a little struct to do it, i.e.:

struct compute_params
{
 int num_threads;
 int s;
}

void* compute(void* thrd)
{	
	compute_params* params = (compute_param*)thrd;
	int s = params.s;
	int num_threads = params.num_threads;
        ...
        free( params );
}

int mcompute(int n, int seed, int itt_max, int num_thread, double *a, double *b)
{
        compute_params* params;
        ...
	for (i = 0; i < num_thread; i++)
	{
		params = malloc( sizeof( compute_params ) );
		params.s = i;
		params.num_threads = num_threads;
		if(pthread_create(&thread[i], NULL, compute, (void*)params) != 0)
		{
			...
		}
	}
	...
}

Open in new window

This way you can pass an arbitrary number of any kind of data to the thread function, so you don't need to have num_threads as global variable anymore.

ZOPPO
0
 

Author Comment

by:crazy4s
ID: 37782243
not sure why i got the error of compute_params  and params undeclared:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include "matmul.h"

void srand48(long int seedval);
double drand48(void);

double *err;

struct compute_params
{
	int s;
	int n;
	int itt_max;
	int num_thread;
	double *a;
	double *b;
}

void* compute(void* thrd)
{	
	int from, to;
	compute_params* params = (compute_params)* thrd;
	int s = params.s;
	int n = params.n;
	int itt_max = params.itt_max;
	int num_thread = params.num_thread;
	double *a = params.a;
	double *b = params.b;

	int i, j;
	double  *t;     // pointer to solution vector, space to be malloced 
	double  *t1;    // pointer to next itteraton of solution vector, 
	                // space to be malloced
	double	*ttemp;	// used to swap t1 and t at each itteration
	int	itt;	// current itteration 
	double	sum;	// computes the inner products for A * t
	double 	error;  // max | t1[i] - t[i] |
	double 	errori; // | t1[i] - t[i] |
	
	if( (t=(double *)malloc(sizeof(double)*n)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for t failed\n");
	}
	
	if( (t1=(double *)malloc(sizeof(double)*n)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for t1 failed\n");
	}
	
	if( (err = (double*) malloc (sizeof (double) * itt_max)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for err failed\n");
	}	

	// Initialize t
	
	for(i=0; i< n; i++) 
	{
  		t[i] = b[i];
	}
	
	if( s < num_thread)
	{
		from = (s * (itt_max + 1))/num_thread;
		to = ((s + 1) * (itt_max + 1))/num_thread - 1;
	}
	else if ( s == num_thread)
	{
		from = (s * (itt_max + 1))/num_thread;
		to = ((s + 1) * (itt_max + 1))/num_thread;
	}

	for(itt=from; itt<=to; itt++) 
	{
  		error=0.0;
  		for(i=0; i< n; i++) 
		{
    			sum = 0.0;
			double *a_itt = a + n*i;
			for(j=0; j< n; j++) 
			{
			   sum += *(a_itt++) * t[j];
			}

    		t1[i] = sum + b[i];
    		errori = fabs(t1[i]-t[i]);
    		if(errori > error) 
			{
      			error=errori;
    		}
  		}
		err[itt] = error;
  		ttemp = t1;
  		t1 = t;
  		t = ttemp;
	}

	pthread_exit(0);
	free(params);
}

int mcompute(int n, int seed, int itt_max, int num_thread, double *a, double *b)
{
	srand48((long int)seed);
	float resultb[n];	
	int	i, j;   // indices into arrays
	pthread_t* thread;	// pointer to a group of threads
	compute_params* params;

	float **resulta = (float**)malloc(sizeof(float*)*n);
	for(i=0; i< n; i++)
		*(resulta+i) = (float*)malloc(sizeof(float)*n);

	for(i=0; i< n; i++) 
	{
  		for(j=0; j< n; j++) 
		{
    			*(a+n*i+j) = 1.999 * (drand48() - 0.5) / n;
    			resulta[i][j] = *(a+n*i+j);
  		}
	}

	moutput_a(resulta, n);

	// Generate vector b 
	for(i=0; i< n; i++) 
	{
  		b[i] = 10.0 * drand48();
		resultb[i] = b[i];
	}

	moutput_b(resultb, n);
	
	thread = (pthread_t*)malloc(num_thread*sizeof(pthread_t));
	
	for (i = 0; i < num_thread; i++)
	{
		params = malloc(sizeof(compute_params));
		params.s = i;
		params.n = n;
		params.itt_max = itt_max;
		params.num_thread = num_thread;
		params.a = a;
		params.b = b;
		if(pthread_create(&thread[i], NULL, compute, (void*)params) != 0)
		{
			perror("Can't create thread!");
			free(thread);
			exit(-1);
		}
	}
	
	for (i = 0; i < num_thread; i++)
		pthread_join (thread[i], NULL);

	moutput_itt(err, itt_max);
	
	free(thread);
	return 0;
}

Open in new window

0
 
LVL 30

Expert Comment

by:Zoppo
ID: 37782254
If this is your exact code then the problem is a missing ;, it has to be
struct compute_params
{
	int s;
	int n;
	int itt_max;
	int num_thread;
	double *a;
	double *b;
}; // <-- here

Open in new window

ZOPPO
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:crazy4s
ID: 37782275
okay even i added the semicolon it's still giving me the same error:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include "matmul.h"

void srand48(long int seedval);
double drand48(void);

double *err;

struct compute_params
{
	int s;
	int n;
	int itt_max;
	int num_thread;
	double *a;
	double *b;
};

void* compute(void* thrd)
{	
	int from, to;
	compute_params* params = (compute_params)* thrd; // struct to be passed
	int s = params.s;
	int n = params.n;
	int itt_max = params.itt_max;
	int num_thread = params.num_thread;
	double *a = params.a;
	double *b = params.b;

	int i, j;
	double  *t;     // pointer to solution vector, space to be malloced 
	double  *t1;    // pointer to next itteraton of solution vector, 
	                // space to be malloced
	double	*ttemp;	// used to swap t1 and t at each itteration
	int	itt;	// current itteration 
	double	sum;	// computes the inner products for A * t
	double 	error;  // max | t1[i] - t[i] |
	double 	errori; // | t1[i] - t[i] |
	
	if( (t=(double *)malloc(sizeof(double)*n)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for t failed\n");
	}
	
	if( (t1=(double *)malloc(sizeof(double)*n)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for t1 failed\n");
	}
	
	if( (err = (double*) malloc (sizeof (double) * itt_max)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for err failed\n");
	}	

	// Initialize t
	
	for(i=0; i< n; i++) 
	{
  		t[i] = b[i];
	}
	
	if( s < num_thread)
	{
		from = (s * (itt_max + 1))/num_thread;
		to = ((s + 1) * (itt_max + 1))/num_thread - 1;
	}
	else if ( s == num_thread)
	{
		from = (s * (itt_max + 1))/num_thread;
		to = ((s + 1) * (itt_max + 1))/num_thread;
	}

	for(itt=from; itt<=to; itt++) 
	{
  		error=0.0;
  		for(i=0; i< n; i++) 
		{
    			sum = 0.0;
			double *a_itt = a + n*i;
			for(j=0; j< n; j++) 
			{
			   sum += *(a_itt++) * t[j];
			}

    		t1[i] = sum + b[i];
    		errori = fabs(t1[i]-t[i]);
    		if(errori > error) 
			{
      			error=errori;
    		}
  		}
		err[itt] = error;
  		ttemp = t1;
  		t1 = t;
  		t = ttemp;
	}

	pthread_exit(0);
	free(params);
}

int mcompute(int n, int seed, int itt_max, int num_thread, double *a, double *b)
{
	srand48((long int)seed);
	float resultb[n];	
	int	i, j;   // indices into arrays
	pthread_t* thread;	// pointer to a group of threads
	compute_params* params;

	float **resulta = (float**)malloc(sizeof(float*)*n);
	for(i=0; i< n; i++)
		*(resulta+i) = (float*)malloc(sizeof(float)*n);

	for(i=0; i< n; i++) 
	{
  		for(j=0; j< n; j++) 
		{
    			*(a+n*i+j) = 1.999 * (drand48() - 0.5) / n;
    			resulta[i][j] = *(a+n*i+j);
  		}
	}

	moutput_a(resulta, n);

	// Generate vector b 
	for(i=0; i< n; i++) 
	{
  		b[i] = 10.0 * drand48();
		resultb[i] = b[i];
	}

	moutput_b(resultb, n);
	
	thread = (pthread_t*)malloc(num_thread*sizeof(pthread_t));
	
	for (i = 0; i < num_thread; i++)
	{
		params = malloc(sizeof(compute_params));
		params.s = i;
		params.n = n;
		params.itt_max = itt_max;
		params.num_thread = num_thread;
		params.a = a;
		params.b = b;
		if(pthread_create(&thread[i], NULL, compute, (void*)params) != 0)
		{
			perror("Can't create thread!");
			free(thread);
			exit(-1);
		}
	}
	
	for (i = 0; i < num_thread; i++)
		pthread_join (thread[i], NULL);

	moutput_itt(err, itt_max);
	
	free(thread);
	return 0;
}

Open in new window

0
 
LVL 30

Expert Comment

by:Zoppo
ID: 37782341
Sorry, I guess the problem is I'm not programming pure C since over 10 years, so I forgot a lot special things.

In C (that's different in C++) you cannot use a struct directly as a variable type. You have either to use the keyword struct for variables too or to use a typedef, i.e.:
struct compute_params
{
	int s;
	int n;
	int itt_max;
	int num_thread;
	double *a;
	double *b;
};

// need to use struct keyword here
struct compute_params params;

Open in new window

or
typedef struct s_compute_params
{
	int s;
	int n;
	int itt_max;
	int num_thread;
	double *a;
	double *b;
} compute_params;

// no need to use struct keyword since it's a typedef
compute_params params;

Open in new window

ZOPPO
0
 

Author Comment

by:crazy4s
ID: 37782376
got these errors:
matmul_compute.c: In function `compute':
matmul_compute.c:25: warning: dereferencing `void *' pointer
matmul_compute.c:25: error: void value not ignored as it ought to be
matmul_compute.c:26: error: request for member `s' in something not a structure or union
matmul_compute.c:27: error: request for member `n' in something not a structure or union
matmul_compute.c:28: error: request for member `itt_max' in something not a structure or union
matmul_compute.c:29: error: request for member `num_thread' in something not a structure or union
matmul_compute.c:30: error: request for member `a' in something not a structure or union
matmul_compute.c:31: error: request for member `b' in something not a structure or union
matmul_compute.c: In function `mcompute':
matmul_compute.c:142: error: request for member `s' in something not a structure or union
matmul_compute.c:143: error: request for member `n' in something not a structure or union
matmul_compute.c:144: error: request for member `itt_max' in something not a structure or union
matmul_compute.c:145: error: request for member `num_thread' in something not a structure or union
matmul_compute.c:146: error: request for member `a' in something not a structure or union
matmul_compute.c:147: error: request for member `b' in something not a structure or union

Open in new window


for the below code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include "matmul.h"

void srand48(long int seedval);
double drand48(void);

double *err;

typedef struct s_compute_params
{
	int s;
	int n;
	int itt_max;
	int num_thread;
	double *a;
	double *b;
} compute_params;

void* compute(void* thrd)
{	
	int from, to;
	compute_params* params = (compute_params)* thrd; // struct to be passed
	int s = params.s;
	int n = params.n;
	int itt_max = params.itt_max;
	int num_thread = params.num_thread;
	double *a = params.a;
	double *b = params.b;

	int i, j;
	double  *t;     // pointer to solution vector, space to be malloced 
	double  *t1;    // pointer to next itteraton of solution vector, 
	                // space to be malloced
	double	*ttemp;	// used to swap t1 and t at each itteration
	int	itt;	// current itteration 
	double	sum;	// computes the inner products for A * t
	double 	error;  // max | t1[i] - t[i] |
	double 	errori; // | t1[i] - t[i] |
	
	if( (t=(double *)malloc(sizeof(double)*n)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for t failed\n");
	}
	
	if( (t1=(double *)malloc(sizeof(double)*n)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for t1 failed\n");
	}
	
	if( (err = (double*) malloc (sizeof (double) * itt_max)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for err failed\n");
	}	

	// Initialize t
	
	for(i=0; i< n; i++) 
	{
  		t[i] = b[i];
	}
	
	if( s < num_thread)
	{
		from = (s * (itt_max + 1))/num_thread;
		to = ((s + 1) * (itt_max + 1))/num_thread - 1;
	}
	else if ( s == num_thread)
	{
		from = (s * (itt_max + 1))/num_thread;
		to = ((s + 1) * (itt_max + 1))/num_thread;
	}

	for(itt=from; itt<=to; itt++) 
	{
  		error=0.0;
  		for(i=0; i< n; i++) 
		{
    			sum = 0.0;
			double *a_itt = a + n*i;
			for(j=0; j< n; j++) 
			{
			   sum += *(a_itt++) * t[j];
			}

    		t1[i] = sum + b[i];
    		errori = fabs(t1[i]-t[i]);
    		if(errori > error) 
			{
      			error=errori;
    		}
  		}
		err[itt] = error;
  		ttemp = t1;
  		t1 = t;
  		t = ttemp;
	}

	pthread_exit(0);
	free(params);
}

int mcompute(int n, int seed, int itt_max, int num_thread, double *a, double *b)
{
	srand48((long int)seed);
	float resultb[n];	
	int	i, j;   // indices into arrays
	pthread_t* thread;	// pointer to a group of threads
	compute_params* params;

	float **resulta = (float**)malloc(sizeof(float*)*n);
	for(i=0; i< n; i++)
		*(resulta+i) = (float*)malloc(sizeof(float)*n);

	for(i=0; i< n; i++) 
	{
  		for(j=0; j< n; j++) 
		{
    			*(a+n*i+j) = 1.999 * (drand48() - 0.5) / n;
    			resulta[i][j] = *(a+n*i+j);
  		}
	}

	moutput_a(resulta, n);

	// Generate vector b 
	for(i=0; i< n; i++) 
	{
  		b[i] = 10.0 * drand48();
		resultb[i] = b[i];
	}

	moutput_b(resultb, n);
	
	thread = (pthread_t*)malloc(num_thread*sizeof(pthread_t));
	
	for (i = 0; i < num_thread; i++)
	{
		params = malloc(sizeof(compute_params));
		params.s = i;
		params.n = n;
		params.itt_max = itt_max;
		params.num_thread = num_thread;
		params.a = a;
		params.b = b;
		if(pthread_create(&thread[i], NULL, compute, (void*)params) != 0)
		{
			perror("Can't create thread!");
			free(thread);
			exit(-1);
		}
	}
	
	for (i = 0; i < num_thread; i++)
		pthread_join (thread[i], NULL);

	moutput_itt(err, itt_max);
	
	free(thread);
	return 0;
}

Open in new window

0
 
LVL 30

Expert Comment

by:Zoppo
ID: 37782390
sorry, didn't see it before - here the cast is wrong:
compute_params* params = (compute_params)* thrd

Open in new window

It has to be
compute_params* params = (compute_params*) thrd

Open in new window

ZOPPO
0
 

Author Comment

by:crazy4s
ID: 37782406
however the rest of the errors are still occur:
matmul_compute.c: In function `compute':
matmul_compute.c:26: error: request for member `s' in something not a structure or union
matmul_compute.c:27: error: request for member `n' in something not a structure or union
matmul_compute.c:28: error: request for member `itt_max' in something not a structure or union
matmul_compute.c:29: error: request for member `num_thread' in something not a structure or union
matmul_compute.c:30: error: request for member `a' in something not a structure or union
matmul_compute.c:31: error: request for member `b' in something not a structure or union
matmul_compute.c: In function `mcompute':
matmul_compute.c:142: error: request for member `s' in something not a structure or union
matmul_compute.c:143: error: request for member `n' in something not a structure or union
matmul_compute.c:144: error: request for member `itt_max' in something not a structure or union
matmul_compute.c:145: error: request for member `num_thread' in something not a structure or union
matmul_compute.c:146: error: request for member `a' in something not a structure or union
matmul_compute.c:147: error: request for member `b' in something not a structure or union

Open in new window

0
 
LVL 30

Expert Comment

by:Zoppo
ID: 37782427
Excuse me, it seems I'm not really concentrated, is a long day already. Of course if you use a pointer (which is correct here) you need to use "->" instead of "." to de-reference values of the struct like:
	int s = params->s;
	int n = params->n;
	int itt_max = params->itt_max;
	int num_thread = params->num_thread;
	double *a = params->a;
	double *b = params->b;

Open in new window

ZOPPO
0
 

Author Comment

by:crazy4s
ID: 37782452
so in mcompute function i should also change from . to ->
            params->s = i;
0
 
LVL 30

Assisted Solution

by:Zoppo
Zoppo earned 500 total points
ID: 37782516
Yes, you need to do it whereever you access members of a struct via a pointer to the struct, otherwise a ".", i.e.:

compute_params params;
params.s = i;
compute_params* params_ptr = &params;
params_ptr->s = i;

Open in new window

0
 

Author Comment

by:crazy4s
ID: 37782634
hmm but in my mcompute function if i use -> i got no errors:
compute_params* params;
for (i = 0; i < num_thread; i++)
	{
		params = malloc(sizeof(compute_params));
		params->s = i;
		params->n = n;
		params->itt_max = itt_max;
		params->num_thread = num_thread;
		params->a = a;
		params->b = b;
		if(pthread_create(&thread[i], NULL, compute, (void*)params) != 0)
		{
			perror("Can't create thread!");
			free(thread);
			exit(-1);
		}
	}

Open in new window


if i use this, i'll get the similar erros as above:
compute_params* params;
for (i = 0; i < num_thread; i++)
	{
		params = malloc(sizeof(compute_params));
		params.s = i;
		params.n = n;
		params.itt_max = itt_max;
		params.num_thread = num_thread;
		params.a = a;
		params.b = b;
		if(pthread_create(&thread[i], NULL, compute, (void*)params) != 0)
		{
			perror("Can't create thread!");
			free(thread);
			exit(-1);
		}
	}

Open in new window

0
 
LVL 30

Expert Comment

by:Zoppo
ID: 37782648
Yes, that's correct, since param is a pointer. In my previous sample I just called the instance param and the pointer param_ptr to show the difference - I usually give my variables names which indicate whether they are pointers or not, often something like 'pParams' or 'ptrParams' or 'ptr_params' or something.

But in your case the pointer's name is 'param', therefor the first version is correct.
0
 

Author Comment

by:crazy4s
ID: 37782670
got it!
i was a little confused at first, but thanks:)
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 37782689
You're welcome,

have a nice day,

best regards,

ZOPPO
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now