Skip to content

Instantly share code, notes, and snippets.

@makotoshimazu
Created December 14, 2015 02:24
Show Gist options
  • Save makotoshimazu/154f98976848409c8a97 to your computer and use it in GitHub Desktop.
Save makotoshimazu/154f98976848409c8a97 to your computer and use it in GitHub Desktop.
mm.c
//! gcc -o mm.bin mm.c -W -Wall -O3 -std=gnu99
/*
* mm.c
*
* Author: Makoto Shimazu <[email protected]>
* URL: https://amiq11.tumblr.com
* License: 2-Clause BSD License
* Created: 2015-12-14
*
*
* Copyright (c) 2015, Makoto Shimazu
* 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.
*
* 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 HOLDER 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.
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
typedef double matrix_t;
matrix_t *matrix_alloc(int nmk) {
matrix_t *m = malloc(sizeof(matrix_t) * nmk * nmk);
if (!m) {
fprintf(stderr, "[%s:%d] allocation failed\n", __func__, __LINE__);
exit(EXIT_FAILURE);
}
return m;
}
void matrix_zero(matrix_t *m, int nmk) {
for (int i = 0; i < nmk; i++)
for (int j = 0; j < nmk; j++)
m[i * nmk + j] = 0;
}
void matrix_set(matrix_t *m, int nmk) {
for (int i = 0; i < nmk; i++)
for (int j = 0; j < nmk; j++)
m[i * nmk + j] = 1.0;
}
double current_sec() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (double)tv.tv_sec + (double)tv.tv_usec * 1.0E-6;
}
int main(int argc, char *argv[]) {
int nmk = 1024;
if (argc >= 2)
nmk = atoi(argv[1]);
printf("== Matrix Multiplication Test ==\n"
"Matrix size: %d\n", nmk);
matrix_t *a = matrix_alloc(nmk);
matrix_t *b = matrix_alloc(nmk);
matrix_t *c = matrix_alloc(nmk);
matrix_set(a, nmk);
matrix_set(b, nmk);
matrix_zero(c, nmk);
double before_sec = current_sec();
for (int i = 0; i < nmk; i++)
for (int j = 0; j < nmk; j++)
for (int k = 0; k < nmk; k++)
c[i * nmk + j] += a[i * nmk + k] * b[k * nmk + j];
double after_sec = current_sec();
double flops = 2.0 * nmk * nmk * nmk;
printf("Elapsed [ms]: %.2f\n"
"Flops [GFLOPS]: %.2f\n",
(after_sec - before_sec) * 1E3,
flops / (after_sec - before_sec) * 1E-9);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment