This repository has been archived by the owner on Nov 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #597 from OP2/outsource-c-functions
Outsource the linear algebra c functions
- Loading branch information
Showing
4 changed files
with
80 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <petscsys.h> | ||
#include <petscblaslapack.h> | ||
|
||
#ifndef PYOP2_WORK_ARRAYS | ||
#define PYOP2_WORK_ARRAYS | ||
#define BUF_SIZE 30 | ||
static PetscBLASInt ipiv_buffer[BUF_SIZE]; | ||
static PetscScalar work_buffer[BUF_SIZE*BUF_SIZE]; | ||
#endif | ||
|
||
static void inverse(PetscScalar* __restrict__ Aout, const PetscScalar* __restrict__ A, PetscBLASInt N) | ||
{ | ||
PetscBLASInt info; | ||
PetscBLASInt *ipiv = N <= BUF_SIZE ? ipiv_buffer : malloc(N*sizeof(*ipiv)); | ||
PetscScalar *Awork = N <= BUF_SIZE ? work_buffer : malloc(N*N*sizeof(*Awork)); | ||
memcpy(Aout, A, N*N*sizeof(PetscScalar)); | ||
LAPACKgetrf_(&N, &N, Aout, &N, ipiv, &info); | ||
if(info == 0){ | ||
LAPACKgetri_(&N, Aout, &N, ipiv, Awork, &N, &info); | ||
} | ||
if(info != 0){ | ||
fprintf(stderr, "Getri throws nonzero info."); | ||
abort(); | ||
} | ||
if ( N > BUF_SIZE ) { | ||
free(Awork); | ||
free(ipiv); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <petscsys.h> | ||
#include <petscblaslapack.h> | ||
|
||
#ifndef PYOP2_WORK_ARRAYS | ||
#define PYOP2_WORK_ARRAYS | ||
#define BUF_SIZE 30 | ||
static PetscBLASInt ipiv_buffer[BUF_SIZE]; | ||
static PetscScalar work_buffer[BUF_SIZE*BUF_SIZE]; | ||
#endif | ||
|
||
static void solve(PetscScalar* __restrict__ out, const PetscScalar* __restrict__ A, const PetscScalar* __restrict__ B, PetscBLASInt N) | ||
{ | ||
PetscBLASInt info; | ||
PetscBLASInt *ipiv = N <= BUF_SIZE ? ipiv_buffer : malloc(N*sizeof(*ipiv)); | ||
memcpy(out,B,N*sizeof(PetscScalar)); | ||
PetscScalar *Awork = N <= BUF_SIZE ? work_buffer : malloc(N*N*sizeof(*Awork)); | ||
memcpy(Awork,A,N*N*sizeof(PetscScalar)); | ||
PetscBLASInt NRHS = 1; | ||
const char T = 'T'; | ||
LAPACKgetrf_(&N, &N, Awork, &N, ipiv, &info); | ||
if(info == 0){ | ||
LAPACKgetrs_(&T, &N, &NRHS, Awork, &N, ipiv, out, &N, &info); | ||
} | ||
if(info != 0){ | ||
fprintf(stderr, "Gesv throws nonzero info."); | ||
abort(); | ||
} | ||
|
||
if ( N > BUF_SIZE ) { | ||
free(ipiv); | ||
free(Awork); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters