How to make doacross loop with -2 dependency - multithreading

I have problem with OpenMP. I have to make doacross loop. For example:
for (int i = 1; i < SIZE-2; i++) {
for (int j = 2; j < SIZE-2; j++) {
tab[i][j] = tab[i][j+2] + tab[i+2][j-2];
}
}
And here I have dependency to the j-2, j+2 and i+2, and I don't know how to resolve this dependency.

You can try something like:
#pragma omp parallel for ordered(2)
for (int i = 1; i < SIZE-2; i++) {
for (int j = 2; j < SIZE-2; j++) {
#pragma omp ordered depend(sink:i,j+2) depend(sink:i+2,j-2)
tab[i][j] = tab[i][j+2] + tab[i+2][j-2];
#pragma omp ordered depend(source)
}
}

I arrived at a working solution based on the answer by dreamcrash:
#pragma omp parallel for ordered(2)
for(int i=1; i<N-2; i++){
for(int j=1; j<N-2; j++){
#pragma omp ordered depend(sink:i,j-2) depend(sink:i-2,j+1)
a[i][j] = a[i][j+2] + a[i+2][j-1];
#pragma omp ordered depend(source)
}
}

Related

Remove the first group of consecutive negative array elements from the array

Delete the first group of consecutive negative
elements in the array.
void delNegative(int* arr, int& size) {
if (!check_prop(arr, size, neg)) return;
int ind_l, ind_f;//
if (arr[0] < 0) ind_f = 0;
if (arr[size - 1] < 0) ind_l = size - 1;
for (int i = 1; i < size; i++){
if (arr[i - 1] > 0 && arr[i] < 0) ind_f = i;
if (arr[i - 1] < 0 && arr[i] > 0) ind_l = i - 1;
}
if (ind_l < ind_f) ind_l = size - 1;
int ii = 0;
for (int i = 0; i < size; i++)
if (i > ind_l || i < ind_f) {
arr[ii] = arr[i];
ii++;
}
size -= ind_l - ind_f + 1;
}
There is this function, but it removes the last group of negative elements.
It needs to be redone for dynamic arrays

Run-Time Check Failure #2 - Stack around the variable 'TP' was corrupted

I'm trying to do a pascal triangle but for some reason i have the error "Run-Time Check Failure #2 - Stack around the variable 'TP' was corrupted." Can someone help me, please?
#include <iostream>
using namespace std;
void main()
{
int TP[100][100] = { 0 }, n;
do
{
cout << "Digite a ordem do triangulo de pascal: ";
cin >> n;
} while (n < 0 || n > 100);
for (int j = 0; j < n; j++)
{
TP[j][0] = 1;
TP[j][j] = 1;
}
for (int i = 2; i < n + 1; i++)
for (int j = 1; TP[i][j] != 1; j++)
TP[i][j] = TP[i - 1][j - 1] + TP[i - 1][j];
for (int i = 0; i < n; i++)
{
cout << endl;
for (int j = 0; j != i + 1; j++)
cout << TP[i][j] << " ";
}
cout << endl << endl;
system("pause");
}
You go beyond the array boundaries in for (int i = 2; i < n + 1; i++) for i == n when n=100.

Why I can duplicate a string by this way but not this way?

This code works fine to me and I can understand it:
char * strduplica(char *s)
{
int i, len = strlen(s);
for (i=0; i<len; i++)
s[i+len] = s[i];
s[i+len] = '\0';
return s;
}
main()
{
char s[20]="Ana";
puts(strduplica(s));
}
Before, I tried this and I got a "Segmentation Fault". Why?:
for (i=0; i<len; i++)
s[len++] = s[i];
s[len] = '\0';
The output should be: "AnaAna".
Because you were incrementing len which is used in the for termination condition:
for (i=0; i<len; i++)
In every iteration both i and len are incremented. Thus, i always stays less than len, and you get an infinite loop.
Eventually, the loop writes a value beyond the allocated area which results in the segmentation fault.

need to input numbers and output as stars c++

this is my first question, i have to write a simple program that asks the user to input an integer, where according to the input, it outputs stars according to the input.
for example:
#include <iostream>
using namespace std;
int main()
{
int n=0;
char star='*';
cout<<"Enter number Desired "<<endl;
cin>> n;
star=n;
cout<<' \n'<<star<<endl;
cout<<' \n'<<star-1<<endl;
cout<<' \n'<<star-2<<endl;
cout<<' \n'<<star-3<<endl;
cout<<' \n'<<star-4<<endl;
system ("pause");
return 0;
}
You should use a for-loop for printing out stars one by one.
An example is given below:
for (int i = 0; i < n; i++) {
cout << "*" << endl;
}
To make this loop print out less and less stars in each row, use nested for-loops:
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
cout << "*" << endl;
}
cout << "\n" << endl;
}
This loop will print out n star characters in the first row, n-1 characters in the second row, and so on.
Let's say, if n == 5, then the output will be:
*****
****
***
**
*
This will print out a descending number of stars from the entered number:
#include <iostream>
using namespace std;
int main() {
int n=0;
char star='*';
cout<<"Enter number Desired "<<endl;
cin>> n;
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
cout << "*";
}
cout << " " << endl;
}
system ("pause");
return 0;
}

time command shows user time greater than real time

I have more or less the same question as
linux time command resulting real is less than user
and
user time larger than real time
but can't post a comment on those questions.
When I run the non-multi-threaded program given below, I occasionally get user time greater than real time with both /usr/bin/time and bash's builtin time. I don't see anything that might use a different core. Is rand() somehow the culprit? How? Thanks!
#include <stdio.h>
#include <stdlib.h>
#define N 100
#define MM_MAX 50000
int
main(int ac, char **av)
{
unsigned int i, j, k, n;
int A[N][N], B[N][N], C[N][N];
if (ac != 2) {
fprintf(stderr, "Usage: matmul <seed>");
exit(1);
}
srand((unsigned int) atoi(av[1]));
for (n = 0; n < atoi(av[1]); n++) {
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
A[i][j] = rand() % MM_MAX;
B[i][j] = rand() % MM_MAX;
}
}
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
C[i][j] = 0;
for (k = 0; k < N; k++) {
C[i][j] += A[i][k] * B[k][j];
}
printf("%7d ", C[i][j]);
}
putchar('\n');
}
}
return 0;
}

Resources