Skip to content

Commit

Permalink
Adding vim modeline for .c files
Browse files Browse the repository at this point in the history
  • Loading branch information
oysteijo committed Nov 21, 2023
1 parent ea7868c commit b5d5db7
Show file tree
Hide file tree
Showing 29 changed files with 184 additions and 96 deletions.
4 changes: 1 addition & 3 deletions examples/custom_activation/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Implementing custom activation functions.

Here is an example og how too implement and use a custom implemented activation function.
Here is an example of how to implement and use a custom implemented activation function.
This example will implement a simple sigmoid function with a pre scale of the input.

$$ f(x) = \frac{1}{1+e^{-\alpha x}} $$
Expand Down Expand Up @@ -48,5 +48,3 @@ You should now be able to specify the custom activation in a neural network like
When setting a loss function with `neuralnet_set_loss()` the derivative will be set correctly if it
is implemented and present in the same shared object file as the forward activation.



4 changes: 4 additions & 0 deletions src/activation.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* activation.c - Øystein Schønning-Johansen 2013 - 2023 */
/*
vim: ts=4 sw=4 softtabstop=4 expandtab
*/
#include "activation.h"
#include "simd.h"

Expand Down
5 changes: 4 additions & 1 deletion src/activation.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* activation.h - Øystein Schønning-Johansen 2013 */
/* activation.h - Øystein Schønning-Johansen 2013 - 2023 */
/*
vim: ts=4 sw=4 softtabstop=4 expandtab
*/
#ifndef __ACTIVATION_H__
#define __ACTIVATION_H__

Expand Down
32 changes: 17 additions & 15 deletions src/callback.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* callback.h - oysteijo@gmail.com
/* callback.h - Øystein Schønning-Johansen 2020 - 2023 */
/*
vim: ts=4 sw=4 softtabstop=4 expandtab
*/

/** \struct _callback_t
Expand Down Expand Up @@ -37,18 +39,18 @@

typedef struct _callback_t callback_t;
struct _callback_t {
void (*callback_run) ( callback_t *self, optimizer_t *opt, const float *result, bool has_valid );
void (*free) (callback_t *self);
void (*callback_run) ( callback_t *self, optimizer_t *opt, const float *result, bool has_valid );
void (*free) (callback_t *self);
};

static inline void callback_run( callback_t *self, optimizer_t *opt, const float *result, bool has_valid )
{
self->callback_run( self, opt, result, has_valid );
self->callback_run( self, opt, result, has_valid );
}

static inline void callback_free( callback_t *self)
{
self->free( self );
self->free( self );
}

#if defined(__GNUC__)
Expand All @@ -66,16 +68,16 @@ static inline void callback_free( callback_t *self)
#define CALLBACK_DEFINE(name,...) \
static void name ## _callback_run( callback_t *cb, optimizer_t *opt, const float *result, bool has_valid ); \
DLLEXPORT name ## _t * name ## _new( void * UNUSED(config)) \
{ \
name ## _t *newcb = malloc( sizeof( name ## _t ) ); \
if ( !newcb ) {\
fprintf( stderr ,"Can't allocate memory for '" #name "_t' callback type.\n"); \
return NULL; \
} \
newcb->cb.callback_run = name ## _callback_run; \
newcb->cb.free = (void(*)(callback_t*)) free; \
__VA_ARGS__ ; \
return newcb; \
{ \
name ## _t *newcb = malloc( sizeof( name ## _t ) ); \
if ( !newcb ) {\
fprintf( stderr ,"Can't allocate memory for '" #name "_t' callback type.\n"); \
return NULL; \
} \
newcb->cb.callback_run = name ## _callback_run; \
newcb->cb.free = (void(*)(callback_t*)) free; \
__VA_ARGS__ ; \
return newcb; \
}

#define CALLBACK_DECLARE(name) \
Expand Down
4 changes: 4 additions & 0 deletions src/earlystopping.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* earlystopping.c - Øystein Schønning-Johansen 2013 - 2023 */
/*
vim: ts=4 sw=4 softtabstop=4 expandtab
*/
#include "earlystopping.h"
#include <stdio.h>
#include <float.h>
Expand Down
4 changes: 4 additions & 0 deletions src/earlystopping.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* earlystopping.h - Øystein Schønning-Johansen 2020 - 2023 */
/*
vim: ts=4 sw=4 softtabstop=4 expandtab
*/
#ifndef __EARLYSTOPPING_H__
#define __EARLYSTOPPING_H__
#include "callback.h"
Expand Down
4 changes: 4 additions & 0 deletions src/evaluate.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* evaluate.c - Øystein Schønning-Johansen 2013 - 2023 */
/*
vim: ts=4 sw=4 softtabstop=4 expandtab
*/
#include "evaluate.h"
#include "simd.h"
#include <string.h>
Expand Down
4 changes: 4 additions & 0 deletions src/evaluate.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* evaluate.h - Øystein Schønning-Johansen 2019 - 2023 */
/*
vim: ts=4 sw=4 softtabstop=4 expandtab
*/
#ifndef __EVALUATE_H__
#define __EVALUATE_H__
#include "neuralnet.h"
Expand Down
4 changes: 4 additions & 0 deletions src/logger.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* logger.c - Øystein Schønning-Johansen 2013 - 2023 */
/*
vim: ts=4 sw=4 softtabstop=4 expandtab
*/
#include "logger.h"
#include "metrics.h"

Expand Down
4 changes: 4 additions & 0 deletions src/logger.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* logger.h - Øystein Schønning-Johansen 2020 - 2023 */
/*
vim: ts=4 sw=4 softtabstop=4 expandtab
*/
#ifndef __LOGGER_H__
#define __LOGGER_H__
#include "callback.h"
Expand Down
4 changes: 4 additions & 0 deletions src/loss.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* loss.c - Øystein Schønning-Johansen 2013 - 2023 */
/*
vim: ts=4 sw=4 softtabstop=4 expandtab
*/
#include "loss.h"

#include <stdint.h>
Expand Down
5 changes: 4 additions & 1 deletion src/loss.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* loss.h - Øystein Schønning-Johansen 2013-2019 */
/* loss.h - Øystein Schønning-Johansen 2013 - 2023 */
/*
vim: ts=4 sw=4 softtabstop=4 expandtab
*/
#ifndef __LOSS_H__
#define __LOSS_H__

Expand Down
114 changes: 59 additions & 55 deletions src/matrix_operations.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* matrix_operations.c - Øystein Schønning-Johansen 2013 - 2023 */
/*
vim: ts=4 sw=4 softtabstop=4 expandtab
*/
#include "matrix_operations.h"
#include "simd.h"
#include <assert.h>
Expand All @@ -14,11 +18,11 @@
#ifdef __AVX__
static inline float horizontalsum_avx( __m256 x )
{
float sumAVX = 0.0f;
__m256 hsum = _mm256_hadd_ps(x, x);
hsum = _mm256_add_ps(hsum, _mm256_permute2f128_ps(hsum, hsum, 0x1));
_mm_store_ss(&sumAVX, _mm_hadd_ps( _mm256_castps256_ps128(hsum), _mm256_castps256_ps128(hsum) ) );
return sumAVX;
float sumAVX = 0.0f;
__m256 hsum = _mm256_hadd_ps(x, x);
hsum = _mm256_add_ps(hsum, _mm256_permute2f128_ps(hsum, hsum, 0x1));
_mm_store_ss(&sumAVX, _mm_hadd_ps( _mm256_castps256_ps128(hsum), _mm256_castps256_ps128(hsum) ) );
return sumAVX;
}
#endif

Expand All @@ -42,24 +46,26 @@ void matrix_vector_multiply( int n_rows, int n_cols, const float *matrix, const
int j = 0;

#ifdef __AVX512F__
__m512 sums = _mm512_setzero_ps ();
for (; j <= ((n_cols)-16); j += 16, m_ptr += 16, v_ptr += 16) /* Check if faster: unroll w prefetch */
#if defined(__FMA__)
sums = _mm512_fmadd_ps( _mm512_loadu_ps(v_ptr), _mm512_load_ps(m_ptr), sums);
#else
sums = _mm512_add_ps (sums, _mm512_mul_ps(_mm512_loadu_ps(v_ptr), _mm512_load_ps(m_ptr)));
#endif
y[i] = _mm512_reduce_add_ps( sums );
__m512 sums = _mm512_setzero_ps ();
for (; j <= ((n_cols)-16); j += 16, m_ptr += 16, v_ptr += 16){ /* Check if faster: unroll w prefetch */
#if defined(__FMA__)
sums = _mm512_fmadd_ps( _mm512_loadu_ps(v_ptr), _mm512_load_ps(m_ptr), sums);
#else
sums = _mm512_add_ps (sums, _mm512_mul_ps(_mm512_loadu_ps(v_ptr), _mm512_load_ps(m_ptr)));
#endif
}
y[i] = _mm512_reduce_add_ps( sums );
#endif
#ifdef __AVX__
__m256 sum = _mm256_setzero_ps ();
for (; j <= ((n_cols)-8); j += 8, m_ptr += 8, v_ptr += 8) /* Check if faster: unroll w prefetch */
#if defined(__FMA__)
sum = _mm256_fmadd_ps( _mm256_load_ps(v_ptr), _mm256_load_ps(m_ptr), sum);
#else
sum = _mm256_add_ps (sum, _mm256_mul_ps(_mm256_load_ps(v_ptr), _mm256_load_ps(m_ptr)));
#endif
y[i] += horizontalsum_avx( sum );
__m256 sum = _mm256_setzero_ps ();
for (; j <= ((n_cols)-8); j += 8, m_ptr += 8, v_ptr += 8){ /* Check if faster: unroll w prefetch */
#if defined(__FMA__)
sum = _mm256_fmadd_ps( _mm256_load_ps(v_ptr), _mm256_load_ps(m_ptr), sum);
#else
sum = _mm256_add_ps (sum, _mm256_mul_ps(_mm256_load_ps(v_ptr), _mm256_load_ps(m_ptr)));
#endif
}
y[i] += horizontalsum_avx( sum );
#endif
for(; j < n_cols; j++ )
y[i] += *v_ptr++ * *m_ptr++;
Expand Down Expand Up @@ -116,68 +122,66 @@ void vector_matrix_multiply( int n, int m, const float *weight, const float *bia
assert( is_aligned( y ));
*/
const float *bias_ptr = bias;
float *y_ptr = y;
float *y_ptr = y;
int i = 0;

#ifdef __AVX512F__
for (; i <= ((m)-16) ; i += 16, bias_ptr +=16, y_ptr +=16 ){
_mm512_store_ps( y_ptr, _mm512_load_ps( bias_ptr ));
for (; i <= ((m)-16) ; i += 16, bias_ptr +=16, y_ptr +=16 ){
_mm512_store_ps( y_ptr, _mm512_load_ps( bias_ptr ));
}
#endif
#ifdef __AVX__
for (; i <= ((m)-8) ; i += 8, bias_ptr +=8, y_ptr +=8 ){
_mm256_store_ps( y_ptr, _mm256_load_ps( bias_ptr ));
for (; i <= ((m)-8) ; i += 8, bias_ptr +=8, y_ptr +=8 ){
_mm256_store_ps( y_ptr, _mm256_load_ps( bias_ptr ));
}
#endif
for( ; i < m; i++ ){
*y_ptr++ = *bias_ptr++;
}

for (int i = 0; i < n; i++) {
float const inp = input[i];
const float *weight_ptr = weight + ( i * m ); /* Argh! if m is not a multiple of ALIGN_SIZE, the pointer wil be unaligned! :-( */
if (inp) {
float *y_ptr = y; /* same goes for this */
if (inp == 1.0f){
for (int i = 0; i < n; i++) {
float const inp = input[i];
const float *weight_ptr = weight + ( i * m ); /* Argh! if m is not a multiple of ALIGN_SIZE, the pointer wil be unaligned! :-( */
if (inp) {
float *y_ptr = y; /* same goes for this */
if (inp == 1.0f){
int j = 0;
#ifdef __AVX512F__
for (; j <= ((m)-16) ; j += 16, y_ptr += 16, weight_ptr += 16)
_mm512_storeu_ps(y_ptr, _mm512_add_ps (_mm512_loadu_ps(y_ptr), _mm512_loadu_ps( weight_ptr )));
for (; j <= ((m)-16) ; j += 16, y_ptr += 16, weight_ptr += 16)
_mm512_storeu_ps(y_ptr, _mm512_add_ps (_mm512_loadu_ps(y_ptr), _mm512_loadu_ps( weight_ptr )));
#endif /* __AVX512F__ */
#ifdef __AVX__
for (; j <= ((m)-8) ; j += 8, y_ptr += 8, weight_ptr += 8)
_mm256_storeu_ps(y_ptr, _mm256_add_ps (_mm256_loadu_ps(y_ptr), _mm256_loadu_ps( weight_ptr )));
for (; j <= ((m)-8) ; j += 8, y_ptr += 8, weight_ptr += 8)
_mm256_storeu_ps(y_ptr, _mm256_add_ps (_mm256_loadu_ps(y_ptr), _mm256_loadu_ps( weight_ptr )));
#endif /* __AVX__ */
for( ; j < m; j++ )
*y_ptr++ += *weight_ptr++;
}

else {
} else {
int j = 0;
#ifdef __AVX512F__
for (; j < ((m)-16) ; j += 16, y_ptr += 16, weight_ptr += 16){
#if defined(__FMA__)
_mm512_storeu_ps(y_ptr, _mm512_fmadd_ps( _mm512_loadu_ps(weight_ptr), _mm512_set1_ps(inp), _mm512_loadu_ps(y_ptr)));
#else
_mm512_storeu_ps(y_ptr, _mm512_add_ps(_mm512_loadu_ps(y_ptr), _mm512_mul_ps(_mm512_loadu_ps(weight_ptr), _mm512_set1_ps(inp))));
#endif
for (; j < ((m)-16) ; j += 16, y_ptr += 16, weight_ptr += 16){
#if defined(__FMA__)
_mm512_storeu_ps(y_ptr, _mm512_fmadd_ps( _mm512_loadu_ps(weight_ptr), _mm512_set1_ps(inp), _mm512_loadu_ps(y_ptr)));
#else
_mm512_storeu_ps(y_ptr, _mm512_add_ps(_mm512_loadu_ps(y_ptr), _mm512_mul_ps(_mm512_loadu_ps(weight_ptr), _mm512_set1_ps(inp))));
#endif
}
#endif
#ifdef __AVX__
__m256 scalevec = _mm256_set1_ps(inp);
for (; j < ((m)-8) ; j += 8, y_ptr += 8, weight_ptr += 8){
#if defined(__FMA__)
_mm256_storeu_ps(y_ptr, _mm256_fmadd_ps( _mm256_loadu_ps(weight_ptr), scalevec, _mm256_loadu_ps(y_ptr)));
#else
_mm256_storeu_ps(y_ptr, _mm256_add_ps(_mm256_loadu_ps(y_ptr), _mm256_mul_ps(_mm256_loadu_ps(weight_ptr), scalevec)));
#endif
__m256 scalevec = _mm256_set1_ps(inp);
for (; j < ((m)-8) ; j += 8, y_ptr += 8, weight_ptr += 8){
#if defined(__FMA__)
_mm256_storeu_ps(y_ptr, _mm256_fmadd_ps( _mm256_loadu_ps(weight_ptr), scalevec, _mm256_loadu_ps(y_ptr)));
#else
_mm256_storeu_ps(y_ptr, _mm256_add_ps(_mm256_loadu_ps(y_ptr), _mm256_mul_ps(_mm256_loadu_ps(weight_ptr), scalevec)));
#endif
}
#endif
for(; j < m; j++ )
*y_ptr++ += inp * *weight_ptr++;
}
}
}
}
}
}
#endif /* USE_CBLAS */
}

Expand Down
4 changes: 4 additions & 0 deletions src/matrix_operations.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* matrix_operations.h - Øystein Schønning-Johansen 2019 - 2023 */
/*
vim: ts=4 sw=4 softtabstop=4 expandtab
*/
#ifndef __MATRIX_OPERATIONS_H__
#define __MATRIX_OPERATIONS_H__

Expand Down
4 changes: 4 additions & 0 deletions src/metrics.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* metrics.c - Øystein Schønning-Johansen 2013 - 2023 */
/*
vim: ts=4 sw=4 softtabstop=4 expandtab
*/
#include "metrics.h"

#include <stdint.h>
Expand Down
5 changes: 4 additions & 1 deletion src/metrics.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* metric.h - Øystein Schønning-Johansen 2013 */
/* metrics.h - Øystein Schønning-Johansen 2019 - 2023 */
/*
vim: ts=4 sw=4 softtabstop=4 expandtab
*/
#ifndef __METRICS_H__
#define __METRICS_H__

Expand Down
4 changes: 4 additions & 0 deletions src/modelcheckpoint.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* modelcheckpoint.c - Øystein Schønning-Johansen 2013 - 2023 */
/*
vim: ts=4 sw=4 softtabstop=4 expandtab
*/
#include "modelcheckpoint.h"
#include "neuralnet.h"
#include "metrics.h"
Expand Down
4 changes: 4 additions & 0 deletions src/modelcheckpoint.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* modelcheckpoint.h - Øystein Schønning-Johansen 2020 - 2023 */
/*
vim: ts=4 sw=4 softtabstop=4 expandtab
*/
#ifndef __MODELCHECKPOINT_H__
#define __MODELCHECKPOINT_H__
#include "callback.h"
Expand Down
6 changes: 4 additions & 2 deletions src/neuralnet.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* Copyright -- Øystein Schønning-Johansen 2007-2023 */

/* neuralnet.c - Øystein Schønning-Johansen 2007 - 2023 */
/*
vim: ts=4 sw=4 softtabstop=4 expandtab
*/
#include "neuralnet.h"
#include "simd.h"
#include "activation.h"
Expand Down
4 changes: 4 additions & 0 deletions src/neuralnet.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* neuralnet.h - Øystein Schønning-Johansen 2007 - 2023 */
/*
vim: ts=4 sw=4 softtabstop=4 expandtab
*/
#ifndef __NN_NEURALNET_H__
#define __NN_NEURALNET_H__

Expand Down
Loading

0 comments on commit b5d5db7

Please sign in to comment.