1 Star 0 Fork 2

gaoxuelong / stress-ng

forked from HoperunHarmony / stress-ng 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
stress-matrix-3d.c 22.85 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
/*
* Copyright (C) 2013-2021 Canonical, Ltd.
* Copyright (C) 2022 Colin Ian King.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "stress-ng.h"
#include "core-put.h"
#include "core-target-clones.h"
#define MIN_MATRIX3D_SIZE (16)
#define MAX_MATRIX3D_SIZE (1024)
#define DEFAULT_MATRIX3D_SIZE (64)
static const stress_help_t help[] = {
{ NULL, "matrix-3d N", "start N workers exercising 3D matrix operations" },
{ NULL, "matrix-3d-ops N", "stop after N 3D maxtrix bogo operations" },
{ NULL, "matrix-3d-method M", "specify 3D matrix stress method M, default is all" },
{ NULL, "matrix-3d-size N", "specify the size of the N x N x N matrix" },
{ NULL, "matrix-3d-zyx", "matrix operation is z by y by x instead of x by y by z" },
{ NULL, NULL, NULL }
};
#if defined(HAVE_VLA_ARG) && \
!defined(__PCC__)
typedef float stress_matrix_3d_type_t;
/*
* the matrix stress test has different classes of maxtrix stressor
*/
typedef void (*stress_matrix_3d_func)(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n]);
typedef struct {
const char *name; /* human readable form of stressor */
const stress_matrix_3d_func func[2]; /* method functions, x by y by z, z by y by x */
} stress_matrix_3d_method_info_t;
static const stress_matrix_3d_method_info_t matrix_3d_methods[];
static int stress_set_matrix_3d_size(const char *opt)
{
size_t matrix_3d_size;
matrix_3d_size = stress_get_uint64(opt);
stress_check_range("matrix-3d-size", matrix_3d_size,
MIN_MATRIX3D_SIZE, MAX_MATRIX3D_SIZE);
return stress_set_setting("matrix-3d-size", TYPE_ID_SIZE_T, &matrix_3d_size);
}
static int stress_set_matrix_3d_zyx(const char *opt)
{
size_t matrix_3d_zyx = 1;
(void)opt;
return stress_set_setting("matrix-3d-zyx", TYPE_ID_SIZE_T, &matrix_3d_zyx);
}
/*
* stress_matrix_3d_xyz_add()
* matrix addition
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_xyz_add(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t i;
for (i = 0; i < n; i++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t k;
for (k = 0; k < n; k++) {
r[i][j][k] = a[i][j][k] + b[i][j][k];
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
}
/*
* stress_matrix_3d_zyx_add()
* matrix addition
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_zyx_add(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t k;
for (k = 0; k < n; k++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t i;
for (i = 0; i < n; i++) {
r[i][j][k] = a[i][j][k] + b[i][j][k];
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
}
/*
* stress_matrix_3d_xyz_sub()
* matrix subtraction
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_xyz_sub(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t i;
for (i = 0; i < n; i++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t k;
for (k = 0; k < n; k++) {
r[i][j][k] = a[i][j][k] - b[i][j][k];
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
}
/*
* stress_matrix_3d_zyx_add()
* matrix subtraction
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_zyx_sub(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t k;
for (k = 0; k < n; k++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t i;
for (i = 0; i < n; i++) {
r[i][j][k] = a[i][j][k] + b[i][j][k];
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
}
/*
* stress_matrix_3d_trans()
* matrix transpose
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_xyz_trans(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n], /* Ignored */
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t i;
(void)b;
for (i = 0; i < n; i++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t k;
for (k = 0; k < n; k++) {
r[i][j][k] = a[k][j][i];
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
}
/*
* stress_matrix_3d_trans()
* matrix transpose
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_zyx_trans(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n], /* Ignored */
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t k;
(void)b;
for (k = 0; k < n; k++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t i;
for (i = 0; i < n; i++) {
r[i][j][k] = a[k][j][i];
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
}
/*
* stress_matrix_3d_mult()
* matrix scalar multiply
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_xyz_mult(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t i;
(void)b;
stress_matrix_3d_type_t v = b[0][0][0];
for (i = 0; i < n; i++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t k;
for (k = 0; k < n; k++) {
r[i][j][k] = v * a[i][j][k];
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
}
/*
* stress_matrix_3d_mult()
* matrix scalar multiply
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_zyx_mult(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t k;
(void)b;
stress_matrix_3d_type_t v = b[0][0][0];
for (k = 0; k < n; k++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t i;
for (i = 0; i < n; i++) {
r[i][j][k] = v * a[i][j][k];
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
}
/*
* stress_matrix_3d_div()
* matrix scalar divide
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_xyz_div(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t i;
(void)b;
stress_matrix_3d_type_t v = b[0][0][0];
for (i = 0; i < n; i++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t k;
for (k = 0; k < n; k++) {
r[i][j][k] = a[i][j][k] / v;
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
}
/*
* stress_matrix_3d_div()
* matrix scalar divide
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_zyx_div(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t k;
(void)b;
stress_matrix_3d_type_t v = b[0][0][0];
for (k = 0; k < n; k++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t i;
for (i = 0; i < n; i++) {
r[i][j][k] = a[i][j][k] / v;
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
}
/*
* stress_matrix_3d_hadamard()
* matrix hadamard product
* (A o B)ij = AijBij
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_xyz_hadamard(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t i;
for (i = 0; i < n; i++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t k;
for (k = 0; k < n; k++) {
r[i][j][k] = a[i][j][k] * b[i][j][k];
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
}
/*
* stress_matrix_3d_hadamard()
* matrix hadamard product
* (A o B)ij = AijBij
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_zyx_hadamard(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t k;
for (k = 0; k < n; k++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t i;
for (i = 0; i < n; i++) {
r[i][j][k] = a[i][j][k] * b[i][j][k];
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
}
/*
* stress_matrix_3d_frobenius()
* matrix frobenius product
* A : B = Sum(AijBij)
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_xyz_frobenius(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t i;
stress_matrix_3d_type_t sum = 0.0;
(void)r;
for (i = 0; i < n; i++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t k;
for (k = 0; k < n; k++) {
sum += a[i][j][k] * b[i][j][k];
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
stress_float_put((float)sum);
}
/*
* stress_matrix_3d_frobenius()
* matrix frobenius product
* A : B = Sum(AijBij)
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_zyx_frobenius(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t k;
stress_matrix_3d_type_t sum = 0.0;
(void)r;
for (k = 0; k < n; k++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t i;
for (i = 0; i < n; i++) {
sum += a[i][j][k] * b[i][j][k];
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
stress_float_put((float)sum);
}
/*
* stress_matrix_3d_copy()
* naive matrix copy, r = a
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_xyz_copy(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t i;
(void)b;
for (i = 0; i < n; i++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t k;
for (k = 0; k < n; k++) {
r[i][j][k] = a[i][j][k];
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
}
/*
* stress_matrix_3d_copy()
* naive matrix copy, r = a
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_zyx_copy(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t k;
(void)b;
for (k = 0; k < n; k++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t i;
for (i = 0; i < n; i++) {
r[i][j][k] = a[i][j][k];
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
}
/*
* stress_matrix_3d_mean(void)
* arithmetic mean
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_xyz_mean(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t i;
for (i = 0; i < n; i++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t k;
for (k = 0; k < n; k++) {
r[i][j][k] = (a[i][j][k] + b[i][j][k]) / (stress_matrix_3d_type_t)2.0;
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
}
/*
* stress_matrix_3d_mean(void)
* arithmetic mean
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_zyx_mean(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t k;
for (k = 0; k < n; k++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t i;
for (i = 0; i < n; i++) {
r[i][j][k] = (a[i][j][k] + b[i][j][k]) / (stress_matrix_3d_type_t)2.0;
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
}
/*
* stress_matrix_3d_zero()
* simply zero the result matrix
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_xyz_zero(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t i;
(void)a;
(void)b;
for (i = 0; i < n; i++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t k;
for (k = 0; k < n; k++) {
r[i][j][k] = 0.0;
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
}
/*
* stress_matrix_3d_zero()
* simply zero the result matrix
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_zyx_zero(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t k;
(void)a;
(void)b;
for (k = 0; k < n; k++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t i;
for (i = 0; i < n; i++) {
r[i][j][k] = 0.0;
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
}
/*
* stress_matrix_3d_negate()
* simply negate the matrix a and put result in r
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_xyz_negate(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t i;
(void)a;
(void)b;
for (i = 0; i < n; i++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t k;
for (k = 0; k < n; k++) {
r[i][j][k] = -a[i][j][k];
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
}
/*
* stress_matrix_3d_negate()
* simply negate the matrix a and put result in r
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_zyx_negate(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t k;
(void)a;
(void)b;
for (k = 0; k < n; k++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t i;
for (i = 0; i < n; i++) {
r[i][j][k] = -a[i][j][k];
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
}
/*
* stress_matrix_3d_identity()
* set r to the identity matrix
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_xyz_identity(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t i;
(void)a;
(void)b;
for (i = 0; i < n; i++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t k;
for (k = 0; k < n; k++) {
r[i][j][k] = ((i == j) && (j == k)) ? 1.0 : 0.0;
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
}
/*
* stress_matrix_3d_identity()
* set r to the identity matrix
*/
static void OPTIMIZE3 TARGET_CLONES stress_matrix_3d_zyx_identity(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
register size_t k;
(void)a;
(void)b;
for (k = 0; k < n; k++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t i;
for (i = 0; i < n; i++) {
r[i][j][k] = ((i == j) && (j == k)) ? 1.0 : 0.0;
}
if (UNLIKELY(!keep_stressing_flag()))
return;
}
}
}
/*
* stress_matrix_3d_all()
* iterate over all cpu stressors
*/
static void OPTIMIZE3 stress_matrix_3d_xyz_all(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
static int i = 1; /* Skip over stress_matrix_3d_all */
matrix_3d_methods[i++].func[0](n, a, b, r);
if (!matrix_3d_methods[i].name)
i = 1;
}
/*
* stress_matrix_3d_all()
* iterate over all cpu stressors
*/
static void OPTIMIZE3 stress_matrix_3d_zyx_all(
const size_t n,
stress_matrix_3d_type_t a[RESTRICT n][n][n],
stress_matrix_3d_type_t b[RESTRICT n][n][n],
stress_matrix_3d_type_t r[RESTRICT n][n][n])
{
static int i = 1; /* Skip over stress_matrix_3d_all */
matrix_3d_methods[i++].func[1](n, a, b, r);
if (!matrix_3d_methods[i].name)
i = 1;
}
/*
* Table of cpu stress methods, ordered x by y by z and z by y by x
*/
static const stress_matrix_3d_method_info_t matrix_3d_methods[] = {
{ "all", { stress_matrix_3d_xyz_all, stress_matrix_3d_zyx_all } },/* Special "all" test */
{ "add", { stress_matrix_3d_xyz_add, stress_matrix_3d_zyx_add } },
{ "copy", { stress_matrix_3d_xyz_copy, stress_matrix_3d_zyx_copy } },
{ "div", { stress_matrix_3d_xyz_div, stress_matrix_3d_zyx_div } },
{ "frobenius", { stress_matrix_3d_xyz_frobenius,stress_matrix_3d_zyx_frobenius } },
{ "hadamard", { stress_matrix_3d_xyz_hadamard, stress_matrix_3d_zyx_hadamard } },
{ "identity", { stress_matrix_3d_xyz_identity, stress_matrix_3d_zyx_identity } },
{ "mean", { stress_matrix_3d_xyz_mean, stress_matrix_3d_zyx_mean } },
{ "mult", { stress_matrix_3d_xyz_mult, stress_matrix_3d_zyx_mult } },
{ "negate", { stress_matrix_3d_xyz_negate, stress_matrix_3d_zyx_negate } },
{ "sub", { stress_matrix_3d_xyz_sub, stress_matrix_3d_zyx_sub } },
{ "trans", { stress_matrix_3d_xyz_trans, stress_matrix_3d_zyx_trans } },
{ "zero", { stress_matrix_3d_xyz_zero, stress_matrix_3d_zyx_zero } },
{ NULL, { NULL, NULL } }
};
static const stress_matrix_3d_method_info_t *stress_get_matrix_3d_method(
const char *name)
{
const stress_matrix_3d_method_info_t *info;
for (info = matrix_3d_methods; info->name; info++) {
if (!strcmp(info->name, name)) {
stress_set_setting("matrix-3d-method", TYPE_ID_STR, name);
return info;
}
}
return NULL;
}
static void stress_matrix_3d_method_error(void)
{
const stress_matrix_3d_method_info_t *info;
(void)fprintf(stderr, "matrix-3d-method must be one of:");
for (info = matrix_3d_methods; info->name; info++)
(void)fprintf(stderr, " %s", info->name);
(void)fprintf(stderr, "\n");
}
/*
* stress_set_matrix_3d_method()
* set the default matrix stress method
*/
static int stress_set_matrix_3d_method(const char *name)
{
const stress_matrix_3d_method_info_t *info;
info = stress_get_matrix_3d_method(name);
if (info) {
stress_set_setting("matrix-3d-method", TYPE_ID_STR, name);
return 0;
}
stress_matrix_3d_method_error();
return -1;
}
static inline size_t round_up(size_t page_size, size_t n)
{
page_size = (page_size == 0) ? 4096 : page_size;
return (n + page_size - 1) & (~(page_size -1));
}
/*
* stress_matrix_data()
*` generate some random data scaled by v
*/
static inline stress_matrix_3d_type_t stress_matrix_data(const stress_matrix_3d_type_t v)
{
const uint64_t r = stress_mwc64();
return v * (stress_matrix_3d_type_t)r;
}
static inline int stress_matrix_3d_exercise(
const stress_args_t *args,
const stress_matrix_3d_func func,
const size_t n)
{
int ret = EXIT_NO_RESOURCE;
typedef stress_matrix_3d_type_t (*matrix_3d_ptr_t)[n][n];
size_t matrix_3d_size = round_up(args->page_size, (sizeof(stress_matrix_3d_type_t) * n * n * n));
matrix_3d_ptr_t a, b = NULL, r = NULL;
register size_t i;
const stress_matrix_3d_type_t v = 65535 / (stress_matrix_3d_type_t)((uint64_t)~0);
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
#if defined(MAP_POPULATE)
flags |= MAP_POPULATE;
#endif
a = (matrix_3d_ptr_t)mmap(NULL, matrix_3d_size,
PROT_READ | PROT_WRITE, flags, -1, 0);
if (a == MAP_FAILED) {
pr_fail("%s: matrix allocation failed, out of memory\n", args->name);
goto tidy_ret;
}
b = (matrix_3d_ptr_t)mmap(NULL, matrix_3d_size,
PROT_READ | PROT_WRITE, flags, -1, 0);
if (b == MAP_FAILED) {
pr_fail("%s: matrix allocation failed, out of memory\n", args->name);
goto tidy_a;
}
r = (matrix_3d_ptr_t)mmap(NULL, matrix_3d_size,
PROT_READ | PROT_WRITE, flags, -1, 0);
if (r == MAP_FAILED) {
pr_fail("%s: matrix allocation failed, out of memory\n", args->name);
goto tidy_b;
}
/*
* Initialise matrices
*/
for (i = 0; i < n; i++) {
register size_t j;
for (j = 0; j < n; j++) {
register size_t k;
for (k = 0; k < n; k++) {
a[i][j][k] = stress_matrix_data(v);
b[i][j][k] = stress_matrix_data(v);
r[i][j][k] = 0.0;
}
}
}
/*
* Normal use case, 100% load, simple spinning on CPU
*/
do {
(void)func(n, a, b, r);
inc_counter(args);
} while (keep_stressing(args));
ret = EXIT_SUCCESS;
(void)munmap((void *)r, matrix_3d_size);
tidy_b:
(void)munmap((void *)b, matrix_3d_size);
tidy_a:
(void)munmap((void *)a, matrix_3d_size);
tidy_ret:
return ret;
}
/*
* stress_matrix()
* stress CPU by doing floating point math ops
*/
static int stress_matrix(const stress_args_t *args)
{
char *matrix_3d_method_name = NULL;
const stress_matrix_3d_method_info_t *matrix_3d_method;
stress_matrix_3d_func func;
size_t matrix_3d_size = 128;
size_t matrix_3d_yx = 0;
int rc;
(void)stress_get_setting("matrix-3d-method", &matrix_3d_method_name);
(void)stress_get_setting("matrix-3d-zyx", &matrix_3d_yx);
matrix_3d_method = stress_get_matrix_3d_method(matrix_3d_method_name);
if (!matrix_3d_method) {
/* Should *never* get here... */
stress_matrix_3d_method_error();
return EXIT_FAILURE;
}
func = matrix_3d_method->func[matrix_3d_yx];
if (args->instance == 0)
pr_dbg("%s using method '%s' (%s)\n", args->name, matrix_3d_method->name,
matrix_3d_yx ? "z by y by x" : "x by y by z");
if (!stress_get_setting("matrix-3d-size", &matrix_3d_size)) {
if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
matrix_3d_size = MAX_MATRIX3D_SIZE;
if (g_opt_flags & OPT_FLAGS_MINIMIZE)
matrix_3d_size = MIN_MATRIX3D_SIZE;
}
stress_set_proc_state(args->name, STRESS_STATE_RUN);
rc = stress_matrix_3d_exercise(args, func, matrix_3d_size);
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
return rc;
}
static void stress_matrix_3d_set_default(void)
{
stress_set_matrix_3d_method("all");
}
static const stress_opt_set_func_t opt_set_funcs[] = {
{ OPT_matrix_3d_method, stress_set_matrix_3d_method },
{ OPT_matrix_3d_size, stress_set_matrix_3d_size },
{ OPT_matrix_3d_zyx, stress_set_matrix_3d_zyx },
{ 0, NULL }
};
stressor_info_t stress_matrix_3d_info = {
.stressor = stress_matrix,
.set_default = stress_matrix_3d_set_default,
.class = CLASS_CPU | CLASS_CPU_CACHE | CLASS_MEMORY,
.opt_set_funcs = opt_set_funcs,
.help = help
};
#else
stressor_info_t stress_matrix_3d_info = {
.stressor = stress_not_implemented,
.class = CLASS_CPU | CLASS_CPU_CACHE | CLASS_MEMORY,
.help = help
};
#endif
1
https://gitee.com/gaoxuelong/stress-ng.git
git@gitee.com:gaoxuelong/stress-ng.git
gaoxuelong
stress-ng
stress-ng
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891