C, pgcc - automatic parallelization "not countable" - c

I use this for loop, which I want to have parallelizide automaticaly, it is used for count of PI number:
piece=1.0/100000;
for (t=0.0; t<1.0; t=t+piece){
x=t+piece/(float)2;
if(x<=1.0){
integral=4/(1+x*x);
sum=sum+integral;
}
}
This is doint partial sum for all values in interval 0-1. Then I made from it PI value. But this is not the problem, problem is, when I use automatic parallelization with pgcc, I set up number of processes but I am told that "Loop not vectorized/parallelized: not countable" when I am compiling my program. I have tried everything, but still no change. Any ideas? Thanks

Your loop variable is a double, try changing the code so it uses an integer:
for (int t = 0; t < 100000; t++) {
x=(t/100000.0)+piece/(float)2;
if(x<=1.0){
integral=4/(1+x*x);
sum=sum+integral;
}
}

I'm guessing this is because your loop counter is a float or double. Try using an integral counter.
int step;
for (step = 0; step < 100000; step++) {
// determine x from step
...
}

Related

What is the correct way of using GMP integer functions in C programs?

I'm trying to calculate the index of Fibonacci number with 1000 digits.
int i = 0, cnt = 2;
mpz_t limit;
mpz_init (limit);
mpz_ui_pow_ui(limit,10UL,999UL);
mpz_t fib[3];
for (i = 0; i < 3; i++)
mpz_init2(fib[i], 1024UL);
mpz_set_ui(fib[0],1UL);
mpz_set_ui(fib[2],1UL);
I think there's something wrong with assigning 1 to 1st and last element. I know that because those elements are not changing. But the loop should be valid till cnt becomes 4782.
The condition in while loop is only satisfied 2 times if.. <=0 or 3 times if .. >=0.
while(mpz_cmp(fib[i],limit)<=0) // should be <= only, not >=
{
i=(i+1)%3;
cnt++;
mpz_add(fib[i],fib[(i+1)%3],fib[(i+2)%3]);
}
for (i = 0; i < 3; i++)
mpz_clear(fib[i]);
mpz_clear(limit);
printf("Fibonacci number with more than 1000 digits: %d\n",cnt);
Please help find the logical error in this (it is compiling perfectly).
P.S. I don't want to use in-built mpz_fib_ui.
Integer Functions
After the for loop, i=3, so the conditional statement for the while loop depends on fib[3]
Adding i=0; before the while loop fixes it, and gives me the desired output:
Fibonacci number with more than 1000 digits: 4782

I cant write this factorial codes

I have some problem with that. I am trying to learn C programming. Please help me
#include<stdio.h>
int main()
{
int a, factorial;
printf("Please enter a value :" );
scanf("%d", &a);
for (int i = 1; i<=a; i++)
{
a = (a - 1)*a;
}
printf("%d", factorial);
return 0;
}
Well in your code line a = (a - 1)*a; you actually changed your input for getting the factorial. It also will blow your loop. See your for loop will continue as long as your i is less than a, lets say you choose a=3 after first iteration the a itself will become 6, so the for loop will continue until it reach the integer limit and you will get overflow error.
What you should do?
First of all you should use a second variable to store the factorial result, you introduced it as factorial, the way that #danielku97 said is a good way to write a factorial since if you present 0 as input it will also give the correct result of 1. so a good code is:
factorial = 1;
for (int i = 1; i<=a; i++)
{
factorial *= i;
}
But lets say you insist of subtraction, the way you just tried to use, then you need to change the code like:
scanf("%d", &a);
if (a==1 || a==0){
printf("1");
return 0;
}
factorial = a;
for (int i = 1; i<a; i++)
{
factorial *= (a - i)*factorial;
}
You can see that the code just got unnecessarily longer. An if included to correct the results for 1 and 0. Also you need to make sure that i never become like i =a since in that case a-i will be equal to zero and will make the factorial result equal to zero.
I hope the explanations can help you on learning C and Algorithm faster.
Your for loop is using your variable 'a' instead of the factorial variable and i, try something like this
factorial = 1;
for (int i = 1; i<=a; i++)
{
factorial *= i;
}
You must initialize your factorial to 1, and then the for loop will keep multiplying it by 'i' until 'i' is greater than 'a'.
You are modifying the input a rather than factorial and also wrong (undefined behaviour) because you are using factorial uninitialized. You simply need to use the factorial variable you declared.
int factorial = 1;
...
for (int i = 1; i<=a; i++) {
factorial = i*factorial;
}
EDIT:
Also, be aware that C's int can only hold limited values. So, beyond a certain number (roughly after 13! if sizeof(int) is 4 bytes), you'll cause integer overflow.
You may want to look at GNU bugnum library for handling large factorial values.

series using program

I'm trying to implement my own sin(x) function via maclaurin series using C program,
this is what I have so far, I worked everything on paper first then I tried to implement it it in a code
#include<stdio.h>
#define PI 3.141592653589793238462643383
int main()
{
int x,nOfterms,term=1,i,j;
double numerator,sum=0.0,radius;
long int denominator;
printf("\n\t\tINPUT:");
printf("\n\t\t------");
printf("\n\t\tEnter the value for x in degrees: ");
scanf("%d",&x);
printf("\n\t\tEnter the value for number of terms: ");
scanf("%d",&nOfterms);
printf("\n\t\tOUTPUT:");
printf("\n\t\t-------");
radius=x*(PI/180.0);
printf("\n\t\tThe radius value for the given x value is: %lf",radius);
for(i=1;i<=nOfterms;i+=2)
{
numerator=1.0;
denominator=1;
for(j=1;j<=i;j++)
{
numerator=numerator*radius;
denominator=denominator*j;
}
sum=(sum+numerator)/(denominator*term);
term=term*-1;
}
printf("\n\t\tThe value for sin %d is: %lf",x,sum);
printf("\n\n\n");
return 0;
}
I can't use the math.h at all in my code or so does the professor say,
My output is all zeros which is not right obviously, also I would appreciate if anyone could help me with this, also if anyone can offer a way where I can see each iteration for each number of term of the series on the screen before I arrive at the answer, tried to place the last printf in the for loop but I'll end up with weird symbols and numbers.
The problem in in the implementation of the Mac Laurin series. The line
sum=(sum+numerator)/(denominator*term);
is wrong. It should be simply :
sum += numerator/denominator*term;
Remaining is fine. I tried it and found sin(30) = 0.5, sin(60) = 0.866 and even sin(90) ~ 1.
But you algorythm is not very efficient since for each term you start back from 1/1 when the first elements of in / i! have already been computed on previous term.
You could change your loops to :
numerator=1.0;
denominator=1;
j = 1;
for(i=1;i<=nOfterms;i+=1)
{
numerator=numerator*radius;
denominator=denominator*j++;
sum += numerator/denominator*term;
term=term*-1;
numerator=numerator*radius;
denominator=denominator*j++;
}
Tested and give same (correct) results.
Here are some problems with your code to help you along:
You want to increment i by 1, not 2 as you count the number of terms.
For each term, you want only the odd powers of x, so it's j you need to increment by 2 (change the upper limit and denominator factorial calculation appropriately)
This line is wrong: sum=(sum+numerator)/(denominator*term); and should presumably be sum=sum+numerator/(denominator*term);
Hope that helps with your assignment.
Your inner for loop is not correct. Manually trace through it for a few values of i:
1: numerator=radius, denominator=1 (correct)
2: numerator=radius, denominator=1 (incorrect)
3: numerator=radius*radius, denominator=3 (incorrect)
Correct inner loop should be:
for (j = 1; j < i*2; ++j)
{
numerator = numerator * radius;
denominator = denominator * j;
}

Infinite for loop in C

No idea what I'm doing wrong here. I just need to know how many times "step" can increment until it reaches 1 -- maybe I'm overcomplicating things... I'm fairly new to programming, and I haven't done any for about a year. I don't know if this is relevant but I'm using Xcode 5.0.2 with what I believe is C11 (very new to this language so I have no idea of the version history).
Anyway, the following throws me into an infinite loop with 100% CPU utilisation and energy impact:
int range = 0;
double step = 0.12;
for(int i = step; i <= 1; i += step)
{
range++;
}
"step" is only equal to 0.12 for the purposes of this example; normally it would be user-created input.
Help? :(
Try this:
int range = 0;
double step = 0.12;
for (double d = step; d <= 1.0; d += step) {
range++;
}
You can't assign a floating-point number to an int and get the result you expect, which is what you're doing in int i=step. Very likely, you will get zero for i. Then, when you increment by step, you will get zero again, which explains the infinite loop.

C programming help - adding values together/for loop

I'm trying to add values together that I get using a for loop but I'm stumped as to how to do it. This is the code I have so far:
float counter;
float harmonic;
float sum;
for (counter = 2; counter <= n; counter ++)
{
harmonic = 1/counter; // current value
sum = harmonic; // stores current value
}
return 0;
}
So basically for each value I get for the "harmonic" variable I need to add it to the next until the loop ends. Should I be looking at arrays? Thanks for any help.
Change your code to read
float sum = 0;
then inside the loop write
sum += harmonic;
You don’t need an array unless you want to remember all of the values you’ve summed.
Also, don’t use a float as your loop counter. You probably want an int instead.

Resources