Site Tools


saxpy

Table of Contents

SAXPY

SAXPY (Scalar Alpha X Plus Y) is a fundamental HPC operation: $\mathbf{y} = a\mathbf{x} + \mathbf{y}$ where $a$ is a scalar and $\mathbf{x}$, $\mathbf{y}$ are vectors. It's embarrassingly parallel—each element can be updated independently—making it a canonical benchmark for measuring parallel overhead.

SAXPY is the prototypical memory-bound operation in BLAS (Basic Linear Algebra Subprograms).

Example

This example shows SAXPY computation and its natural parallelism.

// compile: gcc -fopenmp -o saxpy saxpy.c
// run: ./saxpy
// description: parallel SAXPY: y = a*x + y
 
#include <omp.h>
#include <stdio.h>
 
void saxpy(float* y, const float* x, float a, int n) {
#pragma omp parallel for
    for (int i = 0; i < n; i++) {
        y[i] = a * x[i] + y[i];
    }
}
 
int main() {
    float x[1000], y[1000];
    for (int i = 0; i < 1000; i++) {
        x[i] = i * 0.1f;
        y[i] = i * 0.2f;
    }
 
    saxpy(y, x, 2.5f, 1000);
    printf("y[0] = %f\n", y[0]);
    return 0;
}
saxpy.md · Last modified: by 127.0.0.1