#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;
}
// mathmul.h
...
extern int global_thread_num;
...
// 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
...
}
// 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;
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)
{
...
}
}
...
}
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.#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;
}
struct compute_params
{
int s;
int n;
int itt_max;
int num_thread;
double *a;
double *b;
}; // <-- here
ZOPPO
#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;
}
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;
ortypedef 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;
ZOPPO
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
#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;
}
compute_params* params = (compute_params)* thrd
It has to becompute_params* params = (compute_params*) thrd
ZOPPO
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
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;
ZOPPO
compute_params params;
params.s = i;
compute_params* params_ptr = ¶ms;
params_ptr->s = i;
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);
}
}
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);
}
}
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
Dll Dynamic way c/c++ | 9 | 139 | |
C programming help for beginner sort routine | 5 | 261 | |
Is string constant address ? | 10 | 192 | |
Programming Arduino to control a Max7219 using C | 2 | 114 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
23 Experts available now in Live!