Skip to content

Commit 17a4ca1

Browse files
authored
OpenMP
1 parent 2311581 commit 17a4ca1

File tree

4 files changed

+391
-0
lines changed

4 files changed

+391
-0
lines changed

charfreq_mp.c

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include<stdio.h>
2+
#include <stdlib.h>
3+
#include <time.h>
4+
#include <omp.h>
5+
#include <ctype.h>
6+
7+
int main(int argc, char* argv[])
8+
{
9+
10+
char* buffer;
11+
char keyChar='a';
12+
char fileName[40];
13+
long length;
14+
int threadCount=strtol(argv[1],NULL,10),count=0;
15+
16+
FILE *fp;
17+
18+
printf("Type in file name: \n");
19+
scanf("%s",fileName);
20+
fp = fopen ( fileName, "rb" );
21+
if( !fp ) perror(fileName),exit(1);
22+
23+
fseek( fp , 0L , SEEK_END);
24+
length = ftell( fp );
25+
rewind( fp );
26+
27+
fp = fopen ( fileName, "rb" );
28+
if( !fp ) perror(fileName),exit(1);
29+
30+
/* allocate memory for entire content */
31+
buffer = calloc( 1, length+1 );
32+
if( !buffer ) fclose(fp),fputs("memory alloc fails",stderr),exit(1);
33+
34+
/* copy the file into the buffer */
35+
if( 1!=fread( buffer , length, 1 , fp) )
36+
fclose(fp),free(buffer),fputs("entire read fails",stderr),exit(1);
37+
38+
39+
time_t begin,end;
40+
begin = time(NULL);
41+
42+
43+
# pragma omp parallel for num_threads(threadCount)\
44+
reduction(+: count)
45+
for (int i=0;i<length;i++)
46+
if (buffer[i]==keyChar || tolower(buffer[i])==keyChar)
47+
count++;
48+
free(buffer);
49+
50+
end = time(NULL);
51+
52+
printf("Text length: %ld\nChar '%c' frequency in %s: %d\n",length,keyChar, fileName, count);
53+
double elapsedTime = end-begin;
54+
printf("Elapsed time (seconds): %.2f\n",elapsedTime);
55+
56+
return 0;
57+
}

countSort_mp.c

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <omp.h>
5+
#include <unistd.h>
6+
#include <sched.h>
7+
#include <sys/sysinfo.h>
8+
#include <math.h>
9+
#include <time.h>
10+
11+
12+
13+
14+
void parallelCountSort(int* inputVector, int* countVector, int size, int chunkSize);
15+
16+
17+
int main(int argc, char* argv[])
18+
{
19+
20+
int threadCount, size;
21+
int* inputVector;
22+
int* countVector;
23+
time_t begin,end;
24+
threadCount=strtol(argv[1],NULL,10);
25+
26+
printf("Type in array size: \n");
27+
scanf("%d",&size);
28+
29+
30+
if (size%threadCount!=0 && threadCount>1){
31+
printf("Array size MOD number of processes must be zero or number of processes equal to one.\n");
32+
return 0;
33+
}
34+
35+
36+
inputVector= malloc(size*sizeof(int));
37+
countVector = malloc(size*sizeof(int));
38+
39+
int chunkSize=size/threadCount;
40+
41+
42+
for (int i=0; i<size;i++){
43+
inputVector[i]=rand()%1000;
44+
countVector[i]=-1;
45+
//printf("%d.) %d\n",i+1,inputVector[i]); //for testing purposes
46+
}
47+
48+
begin=time(NULL);
49+
50+
# pragma omp parallel num_threads(threadCount)
51+
parallelCountSort(inputVector,countVector,size,chunkSize);
52+
53+
end = time(NULL);
54+
double elapsedTime = end-begin;
55+
printf("Elapsed time (seconds): %.2f\n",elapsedTime);
56+
// printf("Vector sorted\n");
57+
// for(int i=0;i<size;i++)
58+
// printf("%d.) %d\n",i+1,inputVector[i]);
59+
60+
61+
free(inputVector);
62+
free(countVector);
63+
return 0;
64+
}
65+
66+
void parallelCountSort(int* inputVector, int* countVector, int size, int chunkSize){
67+
68+
69+
int myID=omp_get_thread_num(), count;
70+
int start=myID*chunkSize;;
71+
int end=start+chunkSize;
72+
73+
for (int i=start;i<end;i++){
74+
count=0;
75+
for (int j=0;j<size;j++)
76+
count+=inputVector[j]<inputVector[i] || (inputVector[j]==inputVector[i] && j<i);
77+
countVector[count]=inputVector[i];
78+
}
79+
80+
# pragma omp barrier //enter section once all threads have finished
81+
for(int i=0;i<size;i++)
82+
if (countVector[i]!=-1)
83+
inputVector[i]=countVector[i];
84+
}

gaussElimination_mp.c

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#include<stdio.h>
2+
#include <stdlib.h>
3+
#include<math.h>
4+
#include <time.h>
5+
#include <omp.h>
6+
/*******
7+
Function that performs Gauss-Elimination and returns the Upper triangular matrix and solution of equations:
8+
There are two options to do this in C.
9+
1. Pass the augmented matrix (a) as the parameter, and calculate and store the upperTriangular(Gauss-Eliminated Matrix) in it.
10+
2. Use malloc and make the function of pointer type and return the pointer.
11+
This program uses the first option.
12+
********/
13+
void gaussEliminationLS(int m, int n, double** a, double* x, int threadCount);
14+
void readMatrix(int m, int n, double matrix[m][n]);
15+
void printMatrix(int m, int n, double matrix[m][n]);
16+
void copyMatrix(int m, int n, double** matrix1, double** matrix2);
17+
double** allocateMatrix(int rows, int cols);
18+
19+
int main(int argc, char* argv[]){
20+
int m,n,threadCount=strtol(argv[1],NULL,10);;
21+
22+
printf("Enter the size of the augmeted matrix:\nNo. of rows (m)\n");
23+
scanf("%d",&m);
24+
printf("No.of columns (n)\n");
25+
scanf("%d",&n);
26+
27+
// if (m%threadCount!=0 && threadCount!=1){
28+
// printf("This application is meant to be run with an even number of processes.\n");
29+
// return 0;
30+
// }
31+
32+
double** a=allocateMatrix(m,n);
33+
double* x=malloc(m*sizeof(double));
34+
double** U=allocateMatrix(m,n);
35+
36+
for (int i=0; i<m;i++)
37+
for (int j=0;j<n;j++)
38+
a[i][j]=rand()%1000;
39+
40+
41+
//printf("The original matrix is:\n");
42+
//printMatrix(m,n,a);
43+
//copyMatrix(m,n,a,U);
44+
45+
//Perform Gauss Elimination
46+
time_t begin,end;
47+
begin = time(NULL);
48+
gaussEliminationLS(m,n,a,x, threadCount);
49+
end = time(NULL);
50+
double elapsedTime = end-begin;
51+
printf("Elapsed time (seconds): %.2f\n",elapsedTime);
52+
// printf("\nThe Upper Triangular matrix after Gauss Eliminiation is:\n\n");
53+
// printMatrix(m,n,U);
54+
// printf("\nThe solution of linear equations is:\n\n");
55+
56+
// for(int i=0;i<n-1;i++)
57+
// printf("x[%d]=\t%lf\n",i+1,x[i]);
58+
free(x);
59+
free(a);
60+
free(U);
61+
}
62+
63+
64+
void gaussEliminationLS(int m, int n, double** a, double* x, int threadCount){
65+
int i,j,k;
66+
for(i=0;i<m-1;i++){
67+
if (i%500==0)
68+
printf("Solving ...\n");
69+
//Partial Pivoting
70+
for(k=i+1;k<m;k++){
71+
//If diagonal element(absolute vallue) is smaller than any of the terms below it
72+
if(fabs(a[i][i])<fabs(a[k][i])){
73+
//Swap the rows
74+
for(j=0;j<n;j++){
75+
double temp;
76+
temp=a[i][j];
77+
a[i][j]=a[k][j];
78+
a[k][j]=temp;
79+
}
80+
}
81+
}
82+
83+
double term;
84+
85+
//Begin Gauss Elimination
86+
# pragma omp parallel for num_threads(threadCount) private(k,j,term)
87+
for(k=i+1;k<m;k++){
88+
term=a[k][i]/ a[i][i];
89+
for(j=0;j<n;j++){
90+
a[k][j]=a[k][j]-term*a[i][j];
91+
}
92+
}
93+
94+
}
95+
//Begin Back-substitution
96+
for(i=m-1;i>=0;i--){
97+
x[i]=a[i][n-1];
98+
for(j=i+1;j<n-1;j++){
99+
x[i]=x[i]-a[i][j]*x[j];
100+
}
101+
x[i]=x[i]/a[i][i];
102+
}
103+
104+
}
105+
/*******
106+
Function that reads the elements of a matrix row-wise
107+
Parameters: rows(m),columns(n),matrix[m][n]
108+
*******/
109+
void readMatrix(int m, int n, double matrix[m][n]){
110+
int i,j;
111+
for(i=0;i<m;i++){
112+
for(j=0;j<n;j++){
113+
scanf("%lf",&matrix[i][j]);
114+
}
115+
}
116+
}
117+
/*******
118+
Function that prints the elements of a matrix row-wise
119+
Parameters: rows(m),columns(n),matrix[m][n]
120+
*******/
121+
void printMatrix(int m, int n, double matrix[m][n]){
122+
int i,j;
123+
for(i=0;i<m;i++){
124+
for(j=0;j<n;j++){
125+
printf("%lf\t",matrix[i][j]);
126+
}
127+
printf("\n");
128+
}
129+
}
130+
/*******
131+
Function that copies the elements of a matrix to another matrix
132+
Parameters: rows(m),columns(n),matrix1[m][n] , matrix2[m][n]
133+
*******/
134+
void copyMatrix(int m, int n, double** matrix1, double** matrix2){
135+
int i,j;
136+
for(i=0;i<m;i++){
137+
for(j=0;j<n;j++){
138+
matrix2[i][j]=matrix1[i][j];
139+
}
140+
}
141+
}
142+
143+
double** allocateMatrix(int rows, int cols)
144+
{
145+
double* arr = malloc(rows*cols*sizeof(double));
146+
double** matrix = malloc(rows*sizeof(double*));
147+
int i;
148+
for(i=0; i<rows; i++)
149+
{
150+
matrix[i] = &(arr[i*cols]);
151+
}
152+
return matrix;
153+
}

stringmatch_mp.c

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <omp.h>
5+
#include <unistd.h>
6+
#include <sched.h>
7+
#include <sys/sysinfo.h>
8+
#include <math.h>
9+
#include <time.h>
10+
#include <ctype.h>
11+
12+
13+
int matchString(char* text, char* string, int stringLength, int start, int end);
14+
15+
int main(int argc, char* argv[])
16+
{
17+
char* buffer;
18+
char* keyString="she was";
19+
char fileName[40];
20+
long length;
21+
int threadCount=strtol(argv[1],NULL,10), totalCount=0;
22+
23+
FILE *fp;
24+
25+
printf("Type in file name: \n");
26+
scanf("%s",fileName);
27+
fp = fopen ( fileName, "rb" );
28+
if( !fp ) perror(fileName),exit(1);
29+
30+
fseek( fp , 0L , SEEK_END);
31+
length = ftell( fp );
32+
rewind( fp );
33+
34+
fp = fopen ( fileName, "rb" );
35+
if( !fp ) perror(fileName),exit(1);
36+
37+
/* allocate memory for entire content */
38+
buffer = calloc( 1, length+1 );
39+
if( !buffer ) fclose(fp),fputs("memory alloc fails",stderr),exit(1);
40+
41+
/* copy the file into the buffer */
42+
if( 1!=fread( buffer , length, 1 , fp) )
43+
fclose(fp),free(buffer),fputs("entire read fails",stderr),exit(1);
44+
45+
int stringLength =strlen(keyString);
46+
time_t begin,end;
47+
begin = time(NULL);
48+
49+
if (threadCount==1)
50+
totalCount=matchString(buffer,keyString,stringLength,0,length);
51+
else{
52+
int chunkSize=length/threadCount;
53+
#pragma omp parallel num_threads(threadCount)\
54+
reduction(+: totalCount)
55+
{
56+
int myID=omp_get_thread_num();
57+
int start=myID*chunkSize;
58+
int end=start+chunkSize;
59+
if (myID==threadCount-1)
60+
end+=length%threadCount;
61+
62+
totalCount=matchString(buffer,keyString,stringLength,start,end);
63+
}
64+
}
65+
66+
free(buffer);
67+
68+
end = time(NULL);
69+
70+
printf("File: %s\nText length: %ld\nString pattern \"%s\" matched %d times in text.\n",fileName, length,keyString, totalCount);
71+
double elapsedTime = end-begin;
72+
printf("Elapsed time (seconds): %.2f\n",elapsedTime);
73+
return 0;
74+
}
75+
76+
77+
int matchString(char* text, char* string, int stringLength, int start, int end){
78+
int k=0, count=0, l=start;
79+
80+
while (l<end)
81+
if (tolower(text[l])==tolower(string[k])){
82+
if(k==stringLength-1){
83+
count++;
84+
k=0;
85+
}
86+
else
87+
k++;
88+
l++;
89+
}
90+
else{
91+
k=0;
92+
l++;
93+
}
94+
95+
return count;
96+
97+
}

0 commit comments

Comments
 (0)