Assignment 1: Compiler Switches Not Changing anything

Assignment 1: Compiler Switches Not Changing anything

by Joshua Utley -
Number of replies: 2


I have been attempting to compile my double precision approximation of pi with the different compiler switches in Assignment 1, but the switches do not seem to make any difference in runtime, approximation of pi, or the number of cycles per loop iteration. I'm not exactly sure why it would be doing this, and I can't seem to get a human-readable compiler report with -opt-report5, so I'm at a complete loss. 

My code is here: 

#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

double getTimeStamp();

int main(int argc, char *argv[]) {

    double wcTime,wcTimeStart,wcTimeEnd;

    int SLICES = 1000000000;
    double sum=0., delta_x = 1./1000000000;

    wcTimeStart = getTimeStamp();

    for (int i=0; i < SLICES; i++) {
            double x = (i+0.5)*delta_x;
            double root = sqrt(1.0 - x * x);
            sum += 4.0 * root;
    double Pi = sum * delta_x;

    wcTimeEnd = getTimeStamp();

    wcTime = wcTimeEnd-wcTimeStart;

    //Calculate performance in cy/iteration by multiplying walltime with clock frequency to get total number of cycles.
    //Then divide by the total number of iterations
    double perf = wcTime * 2400000000 * delta_x;

    printf("Walltime: %.3lf s\n",wcTime);
    printf("The value of pi is approximately %lf .", Pi );
    printf("Your cycles/iteration performance is %lf .", perf );

    return 0;

double getTimeStamp() {
    struct timespec ts;
    clock_gettime(CLOCK_MONOTONIC, &ts);
    return (double)ts.tv_sec + (double)ts.tv_nsec * 1.e-9;

In reply to Joshua Utley

Re: Assignment 1: Compiler Switches Not Changing anything

by Georg Hager -

I just tried your code with the options prescribed in Assignment 1. It shows exactly the expected outcome (identical to my own code).

Btw, if you only print 7 digits of pi then you will not see the numerical differences that you are expected to discuss in Problem 1.3(b). To get 16 digits, use "%.16lf" as a format specifier in printf().


In reply to Georg Hager

Re: Assignment 1: Compiler Switches Not Changing anything

by Joshua Utley -
Ah okay. I’ll try to use the longer format.

So when you run that code, a number for the cycles per iteration doesn’t come out to 3? Based on documentation, I would expect the scalar version to require something like 16 cycles per iteration, but it returns 3 even with scalar operations enforced. I’ll keep playing around with it. Thanks for the help!