// OpenMP Implementation
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>
#include <time.h>
#define ROWS 1000
#define COLUMNS 1000
// Function to check if a string is a palindrome
int is_palindrome(char *str, int len) {
for (int i = 0; i < len / 2; i++) {
if (str[i] != str[len - i - 1]) {
return 0;
}
}
return 1;
}
int main() {
char matrix[ROWS][COLUMNS];
int n = 3; // Change this to the palindrome length you want to search for
int total_palindromes = 0;
// Generate the matrix with random characters
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
matrix
[i
][j
] = (rand() % 26) + 'A'; }
}
for (int threads = 1; threads <= 8; threads++) {
omp_set_num_threads(threads);
int palindrome_count = 0;
double start_time = omp_get_wtime();
#pragma omp parallel for reduction(+:palindrome_count)
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
char temp[n + 1];
// Check right-to-left
if (j >= n - 1) {
for (int k = 0; k < n; k++) {
temp[k] = matrix[i][j - k];
}
temp[n] = '\0';
if (is_palindrome(temp, n)) {
palindrome_count++;
}
}
// Check up-to-down
if (i <= ROWS - n) {
for (int k = 0; k < n; k++) {
temp[k] = matrix[i + k][j];
}
temp[n] = '\0';
if (is_palindrome(temp, n)) {
palindrome_count++;
}
}
// Check diagonally up-to-down
if (i <= ROWS - n && j >= n - 1) {
for (int k = 0; k < n; k++) {
temp[k] = matrix[i + k][j - k];
}
temp[n] = '\0';
if (is_palindrome(temp, n)) {
palindrome_count++;
}
}
}
}
double end_time = omp_get_wtime();
total_palindromes = palindrome_count;
printf("%d palindromes of size %d found in %.6f s using %d threads.\n", total_palindromes, n, end_time - start_time, threads);
}
return 0;
}