How can I use ATL_dgemm in C programs on a Macbook>?

63 Views Asked by At

I am studying matrix multiplication in college and I'm trying to test out different forms of this in C. One of these is ATLAS and I am having difficulty getting code which uses ATLAS functions (specifially ATL_dgemm (link)) to run on CLion (or even compile with gcc on a Macbook (running Ventura 13.5.2).

#include <stdio.h>
#include <stdlib.h>
#include <cblas.h>
#include "sys/time.h"

void initMat( int M, int N, double mat[], double val )
{
    int i, j;
    for (i= 0; i< M; i++)
        for (j= 0; j< N; j++)
            mat[i*N+j] = val;
}

int main(void)
{
    int dimensions[] = {1, 5, 10, 100, 200, 400, 500, 1000, 1500, 1600, 1700, 1800, 1900, 2000};
    for (int dimension_index = 0; dimension_index < 14; dimension_index++) {
        int dimension = dimensions[dimension_index];
        double ALPHA = 1.0, BETA = 1.0;
        int n = 1000;
        double *A;
        double *B;
        double *C;
        int numreps = 5;

        int i = 0;
        int j = 0;

        struct timeval tv1, tv2;
        struct timezone tz;

        // allocate memory for the matrices
        // allocate memory for the matrices
        A = malloc(dimension * dimension * sizeof(double));
        B = malloc(dimension * dimension * sizeof(double));
        C = malloc(dimension * dimension * sizeof(double));
        if (!A || !B || !C) {
            printf("Out of memory, reduce N value.\n");
            exit(-1);
        }

        // initialise matrices A and B
        initMat(dimension, dimension, A, rand());
        initMat(dimension, dimension, B, rand());

        //multiply matrices
        printf("Multiply matrices %d times using ATLAS for %d dimensions...\n", numreps, dimension);

        gettimeofday(&tv1, &tz);
        for (i = 0; i < numreps; i++) {
            ATL_dgemm(CblasNoTrans, CblasNoTrans, dimension, dimension, dimension, 1.0, A, dimension, B, dimension, 0.0, C, dimension);
        }
        gettimeofday(&tv2, &tz);
        double elapsed = (double) (tv2.tv_sec - tv1.tv_sec) + (double) (tv2.tv_usec - tv1.tv_usec) * 1.e-6;
        printf("Done ...: time taken for %d dimensions: %fs\n", dimension, elapsed);
        //deallocate memory
        free(A);
        free(B);
        free(C);
    }

    return 0;
}

CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 3.26)
project(atlas C)

set(CMAKE_C_STANDARD 90)

include_directories(
    /usr/local/Cellar/openblas/0.3.24/include
)

add_executable(atlas main.c)

Failure on build/compile looks like this:

error: implicit declaration of function 'ATL_dgemm' [-Werror,-Wimplicit-function-declaration]
ATL_dgemm(CblasNoTrans, CblasNoTrans, dimension, dimension, dimension, 1.0, A, dimension, B, dimension, 0.0, C, dimension);

even when compiling directly like this:

gcc -O3 -I/usr/local/Cellar/openblas/0.3.24/include -o atlas main.c -latlas -lblas -lm
main.c:51:13: error: call to undeclared function 'ATL_dgemm'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
            ATL_dgemm(CblasNoTrans, CblasNoTrans, dimension, dimension, dimension, 1.0, A, dimension, B, dimension, 0.0, C, dimension);
            ^
1 error generated.

This code works on a deployed Linux instance provided by my university but the TAs haven't give me details about how this is configured and I don't have sufficient privileges to dig into how gcc is configured or if ATLAS has been explicitly been installed on it.

I've tried manually installing Atlas using sudo port install atlas +nofortran but this fails with a cryptic enough error:

...
:info:configure ierr=256 in command='make IRun_GAS_x8632 args="-v 5" MYFLAGS="-DATL_OS_OSX" 2> /dev/null | fgrep SUCCESS'!
:info:configure OUTPUT:
:info:configure =======
:info:configure system(make IRun_GAS_x8664 args="-v 5" MYFLAGS="-DATL_OS_OSX" 2> /dev/null | fgrep SUCCESS)
:info:configure ERROR: enum fam=3, chip=2, model=158, mach=0
:info:configure make[3]: *** [atlas_run] Error 44
:info:configure make[2]: *** [IRunArchInfo_x86] Error 2
:info:configure ERROR: enum fam=3, chip=2, model=158, mach=0
:info:configure make[3]: *** [atlas_run] Error 44
:info:configure make[2]: *** [IRunArchInfo_x86] Error 2
:info:configure ERROR: enum fam=3, chip=2, model=158, mach=0
:info:configure make[3]: *** [atlas_run] Error 44
:info:configure make[2]: *** [IRunArchInfo_x86] Error 2
:info:configure cmnd='make IRun_comp args="-v 5 -o atlconf.txt -O 12 -A 28 -Si nof77 1 -V 456  -C ic '/usr/bin/clang' -F ic '-O3 -fomit-frame-pointer -fPIC -march=native -mfma' -C sm '/usr/bin/clang' -F sm '-O3 -fomit-frame-pointer -fPIC -march=native -mfma' -C dm '/usr/bin/clang' -F dm '-O3 -fomit-frame-pointer -fPIC -march=native -mfma' -C sk '/usr/bin/clang' -F sk '-O3 -fomit-frame-pointer -fPIC -march=native -mfma' -C dk '/usr/bin/clang' -F dk '-O3 -fomit-frame-pointer -fPIC -march=native -mfma' -C xc '/usr/bin/clang' -F xc '-O3 -fomit-frame-pointer -fPIC -march=native -mfma' -C gc '/usr/bin/clang' -F gc '-O3 -fomit-frame-pointer -fPIC -march=native -mfma' -b 64"'
:info:configure /usr/bin/clang -I/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_math_atlas/atlas/work/ATLAS/build/..//CONFIG/include  -g -w -DATL_DYLIBS -o xisgcc /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_math_atlas/atlas/work/ATLAS/build/..//CONFIG/src/IsGcc.c atlconf_misc.o 
:info:configure /usr/bin/clang -I/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_math_atlas/atlas/work/ATLAS/build/..//CONFIG/include  -g -w -DATL_DYLIBS -c /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_math_atlas/atlas/work/ATLAS/build/..//CONFIG/src/probe_comp.c
:info:configure /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_math_atlas/atlas/work/ATLAS/build/..//CONFIG/src/probe_comp.c:1165:17: error: call to undeclared function 'CompIsMinGW'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
:info:configure             if (CompIsMinGW(gccs[i]) || CompIsAppleGcc(gccs[i]))
:info:configure                 ^
:info:configure 1 error generated.
:info:configure make: *** [probe_comp.o] Error 1
:info:configure ERROR 512 IN SYSCMND: 'make IRun_comp args="-v 5 -o atlconf.txt -O 12 -A 28 -Si nof77 1 -V 456  -C ic '/usr/bin/clang' -F ic '-O3 -fomit-frame-pointer -fPIC -march=native -mfma' -C sm '/usr/bin/clang' -F sm '-O3 -fomit-frame-pointer -fPIC -march=native -mfma' -C dm '/usr/bin/clang' -F dm '-O3 -fomit-frame-pointer -fPIC -march=native -mfma' -C sk '/usr/bin/clang' -F sk '-O3 -fomit-frame-pointer -fPIC -march=native -mfma' -C dk '/usr/bin/clang' -F dk '-O3 -fomit-frame-pointer -fPIC -march=native -mfma' -C xc '/usr/bin/clang' -F xc '-O3 -fomit-frame-pointer -fPIC -march=native -mfma' -C gc '/usr/bin/clang' -F gc '-O3 -fomit-frame-pointer -fPIC -march=native -mfma' -b 64"'
:info:configure cmnd=make IRun_asm args="-v 5  -O 12" | fgrep 'ASM='
:info:configure Assembly configured as GAS_x8664 (2)
:info:configure Mhz Probe = ' -O 12 -s 2 -m'
:info:configure cmnd=make IRun_arch args="-v 5  -O 12 -s 2 -m" | fgrep 'CPU MHZ='
:info:configure Clock rate configured as 2800Mhz
:info:configure cmnd=make IRun_arch args="-v 5  -O 12 -s 2 -n" | fgrep 'NCPU='
:info:configure Maximum number of threads configured as  8
:info:configure cmnd=make IRun_pmake args="-v 5 -O 12 -t 8" | fgrep 'PMAKE='
:info:configure Parallel make command configured as '$(MAKE) -j 8'
:info:configure cmnd=make IRun_arch args="-v 5  -O 12 -s 2 -t" | fgrep 'CPU THROTTLE='
:info:configure Cannot detect CPU throttling.
:info:configure mkdir src bin tune interfaces
:info:configure cd src ; mkdir testing auxil blas lapack pthreads threads
:info:configure cd src/blas ; \
:info:configure            mkdir f77reference reference gemv ger gemm kbmm \
:info:configure                  level1 level2 level3 pklevel3
:info:configure cd src/blas/reference ; mkdir level1 level2 level3
:info:configure cd src/blas/level2 ; mkdir kernel
:info:configure cd src/blas/pklevel3 ; mkdir gpmm sprk
:info:configure cd src/blas/level3 ; mkdir rblas kernel
:info:configure cd src/pthreads ; mkdir blas misc
:info:configure cd src/pthreads/blas ; mkdir level1 level2 level3
:info:configure cd src/threads ; mkdir blas lapack
:info:configure cd src/threads/blas ; mkdir level3 level2
:info:configure cd tune ; mkdir blas sysinfo lapack threads
:info:configure cd tune/blas ; mkdir gemm gemv ger level1 level3
:info:configure cd interfaces ; mkdir blas lapack
:info:configure cd interfaces/lapack ; mkdir C F77
:info:configure cd interfaces/lapack/C ; mkdir src testing
:info:configure cd interfaces/lapack/F77 ; mkdir src testing
:info:configure cd interfaces/blas ; mkdir C F77
:info:configure cd interfaces/blas/C ; mkdir src testing
:info:configure cd interfaces/blas/F77 ; mkdir src testing
:info:configure cd interfaces/lapack ; mkdir C2F
:info:configure cd interfaces/lapack/C2F ; mkdir src
:info:configure mkdir ARCHS
:info:configure /Library/Developer/CommandLineTools/usr/bin/make -f Make.top startup
:info:configure Make.top:1: Make.inc: No such file or directory
:info:configure Make.top:325: warning: overriding commands for target `/AtlasTest'
:info:configure Make.top:76: warning: ignoring old commands for target `/AtlasTest'
:info:configure make[1]: *** No rule to make target `Make.inc'.  Stop.
:info:configure make: *** [startup] Error 2
:info:configure mv: rename lib/Makefile to lib/Make.tmp: No such file or directory
:info:configure ../configure: line 450: lib/Makefile: No such file or directory
:info:configure ../configure: line 451: lib/Makefile: No such file or directory
:info:configure ../configure: line 452: lib/Makefile: No such file or directory
:info:configure ../configure: line 453: lib/Makefile: No such file or directory
:info:configure ../configure: line 467: lib/Makefile: No such file or directory
:info:configure ../configure: line 468: lib/Makefile: No such file or directory
:info:configure ../configure: line 469: lib/Makefile: No such file or directory
:info:configure ../configure: line 509: lib/Makefile: No such file or directory
:info:configure DONE configure
:debug:configure Executing proc-post-org.macports.configure-configure-0
:debug:configure Patch to recursively remove directories on clean.
:info:configure --->  Patching Makefile: s|rm -f|rm -rf|g
:debug:configure Executing reinplace: /usr/bin/sed {s|rm -f|rm -rf|g} </opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_math_atlas/atlas/work/ATLAS/build/Makefile >@file14
:debug:configure euid/egid changed to: 0/0
:debug:configure chowned /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_math_atlas/atlas/work/ATLAS/build/Makefile to macports
:debug:configure euid/egid changed to: 502/501
:debug:configure euid/egid changed to: 0/0
:debug:configure setting attributes on /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_math_atlas/atlas/work/ATLAS/build/Makefile
:debug:configure euid/egid changed to: 502/501
:debug:configure Executing portconfigure::configure_finish
:debug:configure Privilege de-escalation not attempted as not running as root.
:debug:build build phase started at Thu Oct 19 11:49:17 IST 2023
:notice:build --->  Building atlas
:debug:build Executing org.macports.build (atlas)
:debug:build port disallows a parallel build, setting build jobs to 1
:debug:build port disallows a parallel build, setting build jobs to 1
:debug:build Environment: 
:debug:build CC='/usr/bin/clang'
:debug:build CC_PRINT_OPTIONS='YES'
:debug:build CC_PRINT_OPTIONS_FILE='/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_math_atlas/atlas/work/.CC_PRINT_OPTIONS'
:debug:build CPATH='/opt/local/include'
:debug:build CXX='/usr/bin/clang++'
:debug:build DEVELOPER_DIR='/Library/Developer/CommandLineTools'
:debug:build LIBRARY_PATH='/opt/local/lib'
:debug:build MACOSX_DEPLOYMENT_TARGET='13.0'
:debug:build SDKROOT='/Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk'
:info:build Executing:  cd "/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_math_atlas/atlas/work/ATLAS/build" && /usr/bin/make -j1 -w build 
:debug:build system:  cd "/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_math_atlas/atlas/work/ATLAS/build" && /usr/bin/make -j1 -w build 
:info:build make: Entering directory `/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_math_atlas/atlas/work/ATLAS/build'
:info:build /Library/Developer/CommandLineTools/usr/bin/make -f Make.top build
:info:build make[1]: Entering directory `/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_math_atlas/atlas/work/ATLAS/build'
:info:build Make.top:1: Make.inc: No such file or directory
:info:build Make.top:325: warning: overriding commands for target `/AtlasTest'
:info:build Make.top:76: warning: ignoring old commands for target `/AtlasTest'
:info:build make[1]: *** No rule to make target `Make.inc'.  Stop.
:info:build make[1]: Leaving directory `/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_math_atlas/atlas/work/ATLAS/build'
:info:build make: *** [build] Error 2
:info:build make: Leaving directory `/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_math_atlas/atlas/work/ATLAS/build'
:info:build Command failed:  cd "/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_math_atlas/atlas/work/ATLAS/build" && /usr/bin/make -j1 -w build 
:info:build Exit code: 2
:error:build Failed to build atlas: command execution failed
:debug:build Error code: CHILDSTATUS 21500 2
:debug:build Backtrace: command execution failed
:debug:build     while executing
:debug:build "system {*}$notty {*}$callback {*}$nice $fullcmdstring"
:debug:build     invoked from within
:debug:build "command_exec -callback portprogress::target_progress_callback build"
:debug:build     (procedure "portbuild::build_main" line 8)
:debug:build     invoked from within
:debug:build "$procedure $targetname"
:error:build See /opt/local/var/macports/logs/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_math_atlas/atlas/main.log for details.

Does anyone have explicit guidance on how to coerce a Macbook into recognising these functions and executing this code?

0

There are 0 best solutions below