Jim

A lot of unworked stuff

CC = gcc
CFLAGS=-I. -O2 -std=c99 -pedantic -Wall -g -mavx
%.o: %.c
$(CC) -c -o $@ $< $(CFLAGS)
main: main.o
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
... ...
No preview for this file type
#include <immintrin.h>
#include <stdio.h>
#include <stdlib.h>
//union Mat33 {
// float m[3][3];
// __m128 row[3];
//} Mat33;
struct Mat33 {
float m[3][3];
__m128 row[3];
};
typedef struct Mat33 Mat33;
__m128 lincomb_AVX (const float *a, Mat33 B){
__m128 result;
result = _mm_mul_ps(_mm_broadcast_ss(&a[0]), B.row[0]);
result = _mm_add_ps(result, _mm_mul_ps(_mm_broadcast_ss(&a[1]), B.row[1]));
result = _mm_add_ps(result, _mm_mul_ps(_mm_broadcast_ss(&a[2]), B.row[2]));
return result;
}
void matmult_AVX (Mat33 out, Mat33 A, Mat33 B){
_mm256_zeroupper();
__m128 out0x = lincomb_AVX(A.m[0], B);
__m128 out1x = lincomb_AVX(A.m[1], B);
__m128 out2x = lincomb_AVX(A.m[2], B);
out.row[0] = out0x;
out.row[1] = out1x;
out.row[2] = out2x;
}
void set_mymat (Mat33 M){
for (int i=0; i<3; i++){
for (int j=0; j<3; j++){
M.m[i][j] = 1.0;
printf ("%lf ", M.m[i][j]);
}
printf ("\n");
}
}
void get_mymat (Mat33 M){
for (int i=0; i<3; i++)
for (int j=0; j<3; j++)
printf ("%lf ", M.m[i][j]);
}
int main() {
Mat33 A, B;
set_mymat(A);
set_mymat(B);
// matmult_AVX (out, A, B);
get_mymat (A);
get_mymat (B);
}
... ...
No preview for this file type
CC = gcc
CFLAGS=-I. -O2 -std=c99 -pedantic -Wall -g -mavx
%.o: %.c
$(CC) -c -o $@ $< $(CFLAGS)
main: main.o matrix.o
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
... ...
No preview for this file type
#include <stdio.h>
#include <stdlib.h>
#include <immintrin.h>
union Mat44{
float m[4][4];
__m128 row[4];
} Mat44;
void matmult_SSE(Mat44 *out, const Mat44 *A, const Mat44 *B){
__m128 out0x = lincomb_SSE (A.row[0], B);
__m128 out1x = lincomb_SSE (A.row[1], B);
__m128 out2x = lincomb_SSE (A.row[2], B);
__m128 out3x = lincomb_SSE (A.row[3], B);
out.row[0] = out0x;
out.row[1] = out1x;
out.row[2] = out2x;
out.row[3] = out3x;
}
void run_SSE (Mat44 *out, const Mat44 *A, const Mat44 *B, int count){
for (int i=0; i < count; i++){
int j = i;
matmult_SSE(out[j], A[j], B[j]);
}
}
float randf(){
return (rand() - 16384.0f) / 1024.0f;
}
void randmat (Mat44 &M){
for (int i=0; i < 4; i++)
for (int j=0; j < 4; j++)
M.m[i][j] = randf();
}
void identitymat (Mat44 &M){
for (int i=0; i < 4; i++)
M.m[i][j] = 1;
}
int main(){
Mat44 A;
identitymat(A);
printf ("A: ", A);
return 0;
}
... ...
#include "matrix.h"
#include <stdio.h>
#include <stdlib.h>
int main(){
matrix *m1, *m2, *m3;
m1 = matrix_init (3,3);
m2 = matrix_init (3,3);
for (int i=0; i < matrix_get_rows(m1); i++){
for (int j=0; j < matrix_get_columns(m1); j++) {
set (m1, i, j, 1);
}
}
//we will use identity matrix for learning purposes
matrix_null (m2);
int n = matrix_get_rows (m2);
if (matrix_get_columns(m2) < n )
n = matrix_get_columns (m2);
for (int i=0; i < n; i++){
set(m2, i, i, 1);
}
//m3 = matrix_mult_avx (m1, m2);
test_matrix (m1);
matrix_free (m1);
matrix_free (m2);
// matrix_free (m3);
return 0;
}
... ...
No preview for this file type
#include "matrix.h"
#include <stdio.h>
#include <stdlib.h>
#include <immintrin.h>
matrix* matrix_init(int rows, int columns){
matrix* ret;
if ( (ret = (matrix*) malloc (sizeof(matrix))) == NULL){
return 0;
}
ret->rows = rows;
ret->columns = columns;
if ( (ret->data = (double **) malloc(rows * sizeof(double *))) == NULL ) {
free(ret);
return 0;
}
for (int i = 0; i < rows; i++){
if ( ( ret->data[i] = (double *)malloc (columns * sizeof(double))) == NULL ) {
for (int j = 0; j < i; j++){
free(ret->data[j]);
}
free(ret->data);
free(ret);
return 0;
}
}
return ret;
}
void matrix_free(matrix* m)
{
for (int i = 0; i < m->rows; i++){
free (m->data[i]);
}
free(m->data);
free(m);
}
void set(matrix* m, int i, int j, double value)
{
m->data[i][j] = value;
}
double get(matrix* m, int i, int j){
return m->data[i][j];
}
int matrix_get_rows (matrix *m){
return m->rows;
}
int matrix_get_columns (matrix *m){
return m->columns;
}
void matrix_null (matrix* m){
int rows = matrix_get_rows(m);
int columns = matrix_get_columns(m);
for (int i = 0; i < rows; i++){
for (int j = 0; j < columns; j++){
set (m, i, j, 0);
}
}
}
void matrix_print(matrix* m){
for (int i=0; i < m->rows; i++){
for (int j=0; j < m->columns; j++){
printf ("%lf ", m->data[i][j]);
}
printf("\n");
}
}
matrix* matrix_summ (matrix *m1,matrix *m2){
matrix *m3;
int rows = matrix_get_rows(m1);
int columns = matrix_get_columns(m1);
int m2_columns = matrix_get_columns(m2);
int m2_rows = matrix_get_rows(m2);
//Matrix dimension check
if (rows != m2_rows || columns != m2_columns ){
return 0;
}
m3 = matrix_init(rows, columns);
for (int i = 0; i < rows; i++){
for (int j=0; j < columns; j++){
double res = get(m1, i, j);
res += get(m2, i,j);
set(m3, i, j, res);
}
}
return m3;
}
matrix* matrix_mult (matrix *m1, matrix *m2){
matrix *m3;
int rows = matrix_get_rows(m1);
int columns = matrix_get_columns(m1);
int m2_rows = matrix_get_rows(m2);
int m2_columns = matrix_get_columns(m2);
double temp; //for multiply
//multiply dimension check
if (columns != m2_rows){
return 0;
}
m3 = matrix_init (m2_columns, rows);
for (int i = 0; i < rows; i++){
for (int j = 0; j < m2_columns; j++){
temp = 0;
for (int r = 0; r < columns; r++){
temp += get (m1, i, r) * get (m2, r, j);
}
set (m3, i, j, temp);
}
}
return m3;
}
/*
matrix* matrix_mult_avx (matrix *m1, matrix *m2) {
matrix *m3;
// __m256 ymm0, ymm1, ymm2, ymm3, ymm4, ymm5, ymm6;
// int rows = matrix_get_rows(m1);
// int columns = matrix_get_columns(m1);
// int m2_rows = matrix_get_rows(m2);
// int m2_columns = matrix_get_columns(m2);
double el;
// double *m2_string;
el = get(m1, 1, 1);
printf ("%lf", el);
if (columns != m2_rows){
return 0;
}
*/
// for (int i = 0; i < rows; i++){
// for (int r = 0; r < columns; r++){
// element = get(m1, i, r);
// printf ("Val %lf");
// }
// ymm0 = _mm256_broadcast_ss(element);
// }
//
// for (int i = 0; i < 3; i++){
// m2_string = get (m2, i, 1);
//
void test_matrix (matrix* m1){
double mat_el;
double *result;
result = malloc (4*sizeof(double));
// for (int i = 0; i < m1->rows; i++){
// el = get (m1, i, j);
//
// printf ("%lf ", el);
// }
mat_el = m1->data[1][1];
// const __m256d ymm0 = _mm256_load_pd(&mat_el);
const __m256d ymm0 = _mm256_broadcast_sd(&mat_el);
_mm256_store_pd (result, ymm0);
for (int j = 0; j < 4; j++)
printf ("%lf ", result[j]);
free (result);
// printf ("\n");
}
/*
void matrix_print(matrix* m){
for (int i=0; i < m->rows; i++){
for (int j=0; j < m->columns; j++){
printf ("%lf ", m->data[i][j]);
}
printf("\n");
}
}
*/
... ...
#ifndef MATRIX_H
#define MATRIX_H
struct matrix {
int rows;
int columns;
double **data;
};
typedef struct matrix matrix;
matrix* matrix_init(int rows, int columns);
void matrix_free(matrix* m);
void set(matrix* m, int i, int j, double value);
double get(matrix* m, int i, int j);
int matrix_get_rows (matrix *m);
int matrix_get_columns (matrix *m);
void matrix_null (matrix* m);
void matrix_print(matrix* m);
void test_matrix(matrix* m);
matrix* matrix_summ (matrix *m1,matrix *m2);
matrix* matrix_mult (matrix *m1, matrix *m2);
matrix* matrix_mult_avx (matrix *m1, matrix *m2);
#endif
... ...
No preview for this file type
CC = gcc
CFLAGS=-I. -O2 -std=c99 -pedantic -Wall -g
%.o: %.c
$(CC) -c -o $@ $< $(CFLAGS)
main: main.o matrix.o
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
... ...
No preview for this file type
#include "matrix.h"
#include <stdio.h>
#include <stdlib.h>
int main(){
matrix *m1, *m2, *m3;
m1 = matrix_init (3,3);
m2 = matrix_init (3,3);
for (int i=0; i < matrix_get_rows(m1); i++){
for (int j=0; j < matrix_get_columns(m1); j++) {
set (m1, i, j, 1);
}
}
//we will use identity matrix for learning purposes
matrix_null (m2);
int n = matrix_get_rows (m2);
if (matrix_get_columns(m2) < n )
n = matrix_get_columns (m2);
for (int i=0; i < n; i++){
set(m2, i, i, 1);
}
m3 = matrix_mult (m1, m2);
matrix_print (m1);
matrix_print (m2);
matrix_print (m3);
matrix_free (m1);
matrix_free (m2);
matrix_free (m3);
return 0;
}
... ...
No preview for this file type
#include "matrix.h"
#include <stdio.h>
#include <stdlib.h>
matrix* matrix_init(int rows, int columns){
matrix* ret;
if ( (ret = (matrix*) malloc (sizeof(matrix))) == NULL){
return 0;
}
ret->rows = rows;
ret->columns = columns;
if ( (ret->data = (double **) malloc(rows * sizeof(double *))) == NULL ) {
free(ret);
return 0;
}
for (int i = 0; i < rows; i++){
if ( ( ret->data[i] = (double *)malloc (columns * sizeof(double))) == NULL ) {
for (int j = 0; j < i; j++){
free(ret->data[j]);
}
free(ret->data);
free(ret);
return 0;
}
}
return ret;
}
void matrix_free(matrix* m)
{
for (int i = 0; i < m->rows; i++){
free (m->data[i]);
}
free(m->data);
free(m);
}
void set(matrix* m, int i, int j, double value)
{
m->data[i][j] = value;
}
double get(matrix* m, int i, int j){
return m->data[i][j];
}
int matrix_get_rows (matrix *m){
return m->rows;
}
int matrix_get_columns (matrix *m){
return m->columns;
}
void matrix_null (matrix* m){
int rows = matrix_get_rows(m);
int columns = matrix_get_columns(m);
for (int i = 0; i < rows; i++){
for (int j = 0; j < columns; j++){
set (m, i, j, 0);
}
}
}
void matrix_print(matrix* m){
for (int i=0; i < m->rows; i++){
for (int j=0; j < m->columns; j++){
printf ("%lf ", m->data[i][j]);
}
printf("\n");
}
}
matrix* matrix_summ (matrix *m1,matrix *m2){
matrix *m3;
int rows = matrix_get_rows(m1);
int columns = matrix_get_columns(m1);
int m2_columns = matrix_get_columns(m2);
int m2_rows = matrix_get_rows(m2);
//Matrix dimension check
if (rows != m2_rows || columns != m2_columns ){
return 0;
}
m3 = matrix_init(rows, columns);
for (int i = 0; i < rows; i++){
for (int j=0; j < columns; j++){
double res = get(m1, i, j);
res += get(m2, i,j);
set(m3, i, j, res);
}
}
return m3;
}
matrix* matrix_mult (matrix *m1, matrix *m2){
matrix *m3;
int rows = matrix_get_rows(m1);
int columns = matrix_get_columns(m1);
int m2_rows = matrix_get_rows(m2);
int m2_columns = matrix_get_columns(m2);
double temp; //for multiply
//multiply dimension check
if (columns != m2_rows){
return 0;
}
m3 = matrix_init (m2_columns, rows);
for (int i = 0; i < rows; i++){
for (int j = 0; j < m2_columns; j++){
temp = 0;
for (int r = 0; r < columns; r++){
temp += get (m1, i, r) * get (m2, r, j);
}
set (m3, i, j, temp);
}
}
return m3;
}
... ...
#ifndef MATRIX_H
#define MATRIX_H
struct matrix {
int rows;
int columns;
double **data;
};
typedef struct matrix matrix;
matrix* matrix_init(int rows, int columns);
void matrix_free(matrix* m);
void set(matrix* m, int i, int j, double value);
double get(matrix* m, int i, int j);
int matrix_get_rows (matrix *m);
int matrix_get_columns (matrix *m);
void matrix_null (matrix* m);
void matrix_print(matrix* m);
matrix* matrix_summ (matrix *m1,matrix *m2);
matrix* matrix_mult (matrix *m1, matrix *m2);
#endif
... ...
No preview for this file type
CC = gcc
CFLAGS=-I. -O2 -std=c99 -pedantic -Wall -g
%.o: %.c
$(CC) -c -o $@ $< $(CFLAGS)
main: main.o
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
... ...
No preview for this file type
#include <stdio.h>
#include <stdlib.h>
#define VECTOR_SIZE 4
typedef float v4sf __attribute__ ((vector_size(sizeof(float)*VECTOR_SIZE))); //vector of 4 single floats
union fvector4{
v4sf v;
float f[4];
} fvector4;
int main(){
// v4sf a,b,c,d,e,f,g,h,k;
union fvector4 a, b, c, d, e, f, g, h, i, k, l, m;
//According to issue x[f] allways = 0;