H
here we go
Hello.
I'm new to thread programming and designing algorithims which are
using two cores of CPU. I've written a simple programme that
calculates Fast Walsh Transform and it works great but only on one
core. I've tried many times to rewrite the code for two cores (so that
cpu will be used in 100%) but due to my lack of knowledge in this area
I've failed.
I would really appreciate if someone could rewrite only the algorithm
or give some hints. Below is working code for one core (haven't placed
my attempts for two with pthread_create and other stuff).
Input: length of array (must be power of 2 for e.g 8) and array
created from file. The file contains only numbers 0 and 1 for example
11010011
Output: for input length ile=8 and file containing 11010011 the output
should be: tab=[5 -1 -1 1 1 -1 3 1]
#include <stdio.h>
#include <stdlib.h>
int *tab; //our array
long long int ile; //length of array
int main(void)
{
int ile,i=0,tmp;
FILE *pl;
char c;
printf("Table length: ");
scanf("%d",&ile);
tab = (int*)malloc(ile*sizeof(int*));
pl=fopen("myfile8.txt","r");
c=fgetc(pl);
while(c!=EOF)
{
tmp=atoi(&c);
tab=tmp;
c=fgetc(pl);
i++;
}
long long int a,b,dist,start,block,pair,el1,el2;
dist = 1;
start=ile;
do
{
el1=0; //=0
start=start/2;
for(block=start; block>0; block--)
{
el2=el1+dist;
for(pair=0;pair<dist;pair++)
{
a=tab[el1];
b=tab[el2];
tab[el1]=a+b;
tab[el2]=a-b;
el1++;
el2++;
}
el1=el2;
}
dist=dist*2;
}
while(dist!=ile);
for(i=0;i<ile;i++) printf("tab: %d\n",tab);
return 0;
}
I'm new to thread programming and designing algorithims which are
using two cores of CPU. I've written a simple programme that
calculates Fast Walsh Transform and it works great but only on one
core. I've tried many times to rewrite the code for two cores (so that
cpu will be used in 100%) but due to my lack of knowledge in this area
I've failed.
I would really appreciate if someone could rewrite only the algorithm
or give some hints. Below is working code for one core (haven't placed
my attempts for two with pthread_create and other stuff).
Input: length of array (must be power of 2 for e.g 8) and array
created from file. The file contains only numbers 0 and 1 for example
11010011
Output: for input length ile=8 and file containing 11010011 the output
should be: tab=[5 -1 -1 1 1 -1 3 1]
#include <stdio.h>
#include <stdlib.h>
int *tab; //our array
long long int ile; //length of array
int main(void)
{
int ile,i=0,tmp;
FILE *pl;
char c;
printf("Table length: ");
scanf("%d",&ile);
tab = (int*)malloc(ile*sizeof(int*));
pl=fopen("myfile8.txt","r");
c=fgetc(pl);
while(c!=EOF)
{
tmp=atoi(&c);
tab=tmp;
c=fgetc(pl);
i++;
}
long long int a,b,dist,start,block,pair,el1,el2;
dist = 1;
start=ile;
do
{
el1=0; //=0
start=start/2;
for(block=start; block>0; block--)
{
el2=el1+dist;
for(pair=0;pair<dist;pair++)
{
a=tab[el1];
b=tab[el2];
tab[el1]=a+b;
tab[el2]=a-b;
el1++;
el2++;
}
el1=el2;
}
dist=dist*2;
}
while(dist!=ile);
for(i=0;i<ile;i++) printf("tab: %d\n",tab);
return 0;
}