Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lapack/potrf/potrf_L_single.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,12 @@ blasint CNAME(blas_arg_t *args, BLASLONG *range_m, BLASLONG *range_n, FLOAT *sa,
a + (is + j * lda) * COMPSIZE, lda, 0);

if (is < j + bk + min_j) {
GEMM_OTCOPY(bk, min_i, a + (is + j * lda) * COMPSIZE, lda, sb2 + bk * (is - j - bk) * COMPSIZE);
/* Pack only the rows of the first tile: sb2 has room for min_j of
them, and the SYRK kernel below reads no more than that. */
BLASLONG min_ii = j + bk + min_j - is;
if (min_ii > min_i) min_ii = min_i;

GEMM_OTCOPY(bk, min_ii, a + (is + j * lda) * COMPSIZE, lda, sb2 + bk * (is - j - bk) * COMPSIZE);
}

SYRK_KERNEL_L(min_i, min_j, bk, dm1,
Expand Down
4 changes: 4 additions & 0 deletions utest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ set(OpenBLAS_utest_src
${OpenBLAS_utest_src}
test_potrs.c
)
# test_potrf_workarea.c calls the single-threaded POTRF driver directly, which a shared libopenblas does not export
if (BUILD_STATIC_LIBS)
set(OpenBLAS_utest_src ${OpenBLAS_utest_src} test_potrf_workarea.c)
endif ()
set(OpenBLAS_utest_ext_src
${OpenBLAS_utest_ext_src}
${DIR_EXT}/test_cspmv.c
Expand Down
2 changes: 1 addition & 1 deletion utest/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ OBJS_EXT+=$(DIR_EXT)/test_zgemm.o $(DIR_EXT)/test_cgemm.o $(DIR_EXT)/test_zgbmv.
OBJS_EXT+=$(DIR_EXT)/test_bgemm.o

ifneq ($(NO_LAPACK), 1)
OBJS += test_potrs.o
OBJS += test_potrs.o test_potrf_workarea.o
OBJS_EXT += $(DIR_EXT)/test_zspmv.o $(DIR_EXT)/test_cspmv.o $(DIR_EXT)/test_zsbmv.o $(DIR_EXT)/test_csbmv.o
ifneq ($(NO_CBLAS), 1)
ifneq ($(NO_LAPACKE), 1)
Expand Down
193 changes: 193 additions & 0 deletions utest/test_potrf_workarea.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/*****************************************************************************
Copyright (c) 2026, The OpenBLAS Project
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. Neither the name of the OpenBLAS project nor the names of
its contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**********************************************************************************/

/* The single-threaded lower-triangular POTRF driver
* (lapack/potrf/potrf_L_single.c) packs the rows of the "first tile" of the
* trailing matrix behind sb2 in its work area. sb2 has room for REAL_GEMM_R
* rows of GEMM_Q elements, but the rows were packed GEMM_P at a time without
* stopping at the end of the tile, so up to GEMM_P - 1 rows too many were
* written: past the end of the BUFFER_SIZE work area once n - GEMM_Q exceeds
* REAL_GEMM_R.
*
* This test calls the driver directly with a work area of its own, laid out
* like the one interface/lapack/potrf.c gets from blas_memory_alloc():
* BUFFER_SIZE bytes, page aligned (but not aligned to GEMM_ALIGN + 1, which
* blas_memory_alloc() does not promise either), followed by PW_TAIL bytes
* that must stay zero. The matrix is
*
* [ I E^T ] I : GEMM_Q x GEMM_Q identity
* [ E 0 ] E : all ones
*
* so the driver factors the first diagonal block, updates the whole
* trailing matrix once (which is where the first tile is packed) and stops
* at the next pivot, which is -GEMM_Q: info = GEMM_Q + 1. That costs about
* n * n * GEMM_Q / 2 multiply-adds and 8 * n * n bytes of zero-initialized
* memory, of which about half is touched. n grows with BUFFER_SIZE (about
* 10^4 for 32 MB work areas, 4 * 10^4 for the 128 MB default of x86_64), so
* the test is skipped where it would need more than PW_MAX_BYTES or more
* than OPENBLAS_UTEST_TIME_LIMIT seconds (default 10, 0 = no limits).
*
* It needs the internal driver symbol, i.e. a static libopenblas
* (utest/CMakeLists.txt adds it only with BUILD_STATIC_LIBS). */

#include "openblas_utest.h"
#include <cblas.h>
#include <stdio.h>
#include <string.h>

#if defined(BUILD_DOUBLE)

#if !defined(_WIN32) || defined(__CYGWIN__)
#include <sys/time.h>
#endif

#define PW_TAIL (4 << 20) /* zero memory behind the work area that is checked */
#define PW_ALIGN (64 * 1024)
#define PW_PAGE 4096
#define PW_MAX_BYTES ((double)(1 << 30)) /* largest matrix allocated unless the limits are off */

static double pw_now(void)
{
#if defined(_WIN32) && !defined(__CYGWIN__)
LARGE_INTEGER f, c;
QueryPerformanceFrequency(&f); QueryPerformanceCounter(&c);
return (double)c.QuadPart / (double)f.QuadPart;
#else
struct timeval t;
gettimeofday(&t, NULL);
return (double)t.tv_sec + 1e-6 * (double)t.tv_usec;
#endif
}

/* One call of the driver on the matrix described above; returns info, or -1 without memory. */
static blasint pw_run(BLASLONG n, BLASLONG q, char *area, BLASLONG sb_off, double *secs)
{
blas_arg_t args;
blasint info;
BLASLONG i, j;
double t0, *a = (double *)calloc((size_t)n * (size_t)n, sizeof(double));
if (!a) return -1;
for (j = 0; j < q; j++) {
a[j + j * n] = 1.0;
for (i = q; i < n; i++) a[i + j * n] = 1.0; /* no zeros in what gets packed */
}
memset(&args, 0, sizeof(args));
args.n = n; args.a = a; args.lda = n;
args.nthreads = 1;
t0 = pw_now();
info = dpotrf_L_single(&args, NULL, NULL, (double *)(area + GEMM_OFFSET_A), (double *)(area + sb_off), 0);
*secs = pw_now() - t0;
free(a);
return info;
}

CTEST(potrf, single_work_area_bound)
{
BLASLONG p = DGEMM_P, q = DGEMM_Q, r = DGEMM_R, pq, real_r, sb_off, n, n0;
BLASULONG sb2;
double limit = 10.0, secs = 0.0, est, bytes;
blasint info;
long past = -1;
size_t o;
char *raw, *area, *env;

if (p < 1 || p > 65536 || q < 1 || q > 65536 || r < 1) {
CTEST_LOG("skipped: implausible GEMM parameters (built with other flags than the library?)");
return;
}
pq = p > q ? p : q;
real_r = (r > pq + 96 ? r : pq + 96) - pq; /* REAL_GEMM_R of the driver */
sb_off = (BLASLONG)GEMM_OFFSET_A + (BLASLONG)GEMM_OFFSET_B
+ (BLASLONG)(((size_t)p * q * sizeof(double) + GEMM_ALIGN) & ~(size_t)GEMM_ALIGN);
n = q + real_r + p; /* first tile of real_r rows, then at least one more block of rows */
n0 = 4 * q + 8; /* smallest size with the same blocking, for the time estimate */
if (n < n0) {
CTEST_LOG("skipped: GEMM_R=%ld is too small for this test", (long)r);
return;
}

env = getenv("OPENBLAS_UTEST_TIME_LIMIT");
if (env) limit = atof(env);
bytes = (double)n * (double)n * sizeof(double);
if (limit > 0 && bytes > PW_MAX_BYTES) {
CTEST_LOG("skipped: n=%ld needs a matrix of %.1f GB", (long)n, bytes / 1e9);
return;
}

raw = (char *)calloc(1, (size_t)BUFFER_SIZE + PW_TAIL + PW_ALIGN + PW_PAGE);
if (!raw) {
CTEST_LOG("skipped: not enough memory");
return;
}
area = (char *)(((BLASULONG)raw + PW_ALIGN - 1) & ~(BLASULONG)(PW_ALIGN - 1)) + PW_PAGE;

/* where the driver puts sb2, and whether real_r rows fit behind it at all */
sb2 = (((BLASULONG)(area + sb_off) + (BLASULONG)pq * q * sizeof(double) + GEMM_ALIGN) & ~(BLASULONG)GEMM_ALIGN) + GEMM_OFFSET_B;
if ((double)(sb2 - (BLASULONG)area) + (double)q * (double)real_r * sizeof(double) > (double)BUFFER_SIZE) {
free(raw);
CTEST_LOG("skipped: GEMM_R=%ld leaves no room for GEMM_ALIGN in this work area", (long)r);
return;
}

info = pw_run(n0, q, area, sb_off, &secs);
if (info < 0) {
free(raw);
CTEST_LOG("skipped: not enough memory");
return;
}
ASSERT_EQUAL(q + 1, info);
est = secs * ((double)n / (double)n0) * ((double)n / (double)n0);
if (limit > 0 && est > limit) {
free(raw);
CTEST_LOG("skipped: n=%ld would take about %.0f s", (long)n, est);
return;
}

info = pw_run(n, q, area, sb_off, &secs);
if (info >= 0)
for (o = 0; o < PW_TAIL; o++)
if (area[(size_t)BUFFER_SIZE + o]) { past = (long)o; break; }
free(raw);
if (info < 0) {
CTEST_LOG("skipped: not enough memory for n=%ld", (long)n);
return;
}

CTEST_LOG("dpotrf_L_single n=%ld, GEMM_P=%ld GEMM_Q=%ld GEMM_R=%ld, BUFFER_SIZE=%lu: %.2f s",
(long)n, (long)p, (long)q, (long)r, (unsigned long)BUFFER_SIZE, secs);
if (past >= 0)
CTEST_LOG("the driver wrote past the end of its work area (first changed byte at +%ld)", past);
ASSERT_EQUAL(-1, past); /* nothing written behind the BUFFER_SIZE work area */
ASSERT_EQUAL(q + 1, info); /* and the driver went through the trailing update */
}

#endif
Loading