-2

Can anyone tell me why the output ( Anew ) is:

Anew = 0.000000    
Anew = 2.000000    
Anew = 4.000000    
Anew = 6.000000    
Anew = 16.000000    
Anew = 20.000000    
Anew = 24.000000    
Anew = 28.000000

and not :

Anew = 0.000000    
Anew = 2.000000    
Anew = 4.000000    
Anew = 6.000000    
Anew = 8.000000    
Anew = 10.000000    
Anew = 12.000000    
Anew = 14.000000

Code:

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

int main(int argc, const char* argv[]) {
    int rows = 2, cols = 2, Layers = 2;
    int PerLayerElmts = rows * cols;

    float* A = malloc(PerLayerElmts * Layers * sizeof(*A));

    // create A matrix
    int ImagIdx;
    for (int n = 0; n < Layers; n++) {
        ImagIdx = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                A[n * PerLayerElmts + ImagIdx] = n * PerLayerElmts + ImagIdx;
                ImagIdx++;
            }
        }
    }
    // print A matrix
    for (int n = 0; n < Layers; n++) {
        ImagIdx = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                printf("\nA = %f", A[n * PerLayerElmts + ImagIdx]);
                ImagIdx++;
            }
        }
    }
    float scalar = 2.0;
    size_t AddressOffset = 0;
    for (int i = 0; i < Layers; i++, AddressOffset += PerLayerElmts) {
        // multiply A by scalar
        cblas_sscal(PerLayerElmts * Layers, scalar, A + AddressOffset, 1);
    }
    // print A matrix
    for (int n = 0; n < Layers; n++) {
        ImagIdx = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                printf("\n\nAnew = %f", A[n * PerLayerElmts + ImagIdx]);
                ImagIdx++;
            }
        }
    }

    printf("\n");

    free(A);

    return 0;
}

I am just creating a matrix and then using cblas call to multiply every element by a scalar ( 2 ).

I am doing that , using an addressoffset for the number of layers.

The problem is at the second layer where the elements are multiply by 4 and not 2!

NetVipeC
  • 4,402
  • 1
  • 17
  • 19
George
  • 5,808
  • 15
  • 83
  • 160
  • 4
    Have you tried stepping through your code in a debugger? – dandan78 Oct 03 '14 at 13:58
  • @dandan78:Hmm , I tried and I can see that the A values are the correct values from cblas call.So , the error must come from the kast print statement?But ,I can't figure it!! – George Oct 03 '14 at 14:11

1 Answers1

1

Your way of calling cblas_sscal doesn't seem right. Instead of

cblas_sscal( PerLayerElmts * Layers , scalar , A + AddressOffset , 1 );

I would expect something like

cblas_sscal( PerLayerElmts , scalar , A + AddressOffset , 1 );

since you're calling it once for every layer.

downhillFromHere
  • 1,967
  • 11
  • 11