Skip to content

Instantly share code, notes, and snippets.

@piotrMocz
piotrMocz / typeclasses.scala
Last active August 29, 2015 14:19
Typeclasses vs. Overloading vs. Inheritance vs. Monkeypatching
/**
* Przykład w Scali, bo to jedyny znany mi język, który udostępnia i dziedziczenie, i typeclassy.
* Jest prosty, więc nie trzeba znać ani troche Scali :)
*
* tl;dr Typeclassy są super, ale alternatywą są interfejsy, a nie przeciążanie.
*/
// sytuacja trochę jak z Accelerate'em: biblioteka do algebry liniowej, czyli udostępnia jakiś typ macierzowy (A jest parametrem typu):
class Matrix[A](values: Array[A], rows: Int, cols: Int)
@piotrMocz
piotrMocz / DispatchTest.java
Created April 13, 2015 22:24
Single vs. Multiple dispatch
// przyklad ma na celu zobrazowanie czym rozni sie multiple dispatch od single dispatch.
public class DispatchTest {
public static void main(String[] args) {
Foo foo = new Bar(); // typ statyczny: Foo, typ runtime'owy: Bar
A a = new B(); // typ statyczny: A, typ runtime'owy: B
foo.fooMethod(a); // wolamy tak, by dac szanse kompilatorowi sie wykazac (dwa razy inny typ
// dynamiczny i statyczny)
}
}
@piotrMocz
piotrMocz / bidiagonalization.scala
Created August 4, 2014 19:22
This is more or less what despair looks like in code.
/**
* Bidiagonalization with removed calls to my own kernels, inlined functions for
* calculating the householder vectors and a few other minor changes.
*/
def bidiagDoubleInline(A: CuMatrix[Double])(implicit handle: cublasHandle): (CuMatrix[Double], CuMatrix[Double], CuMatrix[Double]) = {
if (A.rows < A.cols) {
println("Number of rows of matrix A cannot be smaller than the number of columns.")
return (null, A, null)
}
@piotrMocz
piotrMocz / lu_getrf.scala
Created June 8, 2014 20:04
Finally working -- only square matrices though
def LU(A: CuMatrix[Float])(implicit handle: cublasHandle): CuMatrix[Float]= {
if (A.rows != A.cols) {
println("Matrix has to be square.")
return A
}
val d_A = CuMatrix.create[Float](A.rows, A.cols)
d_A := A
val P = CuMatrix.create[Int](d_A.rows, 1)
@piotrMocz
piotrMocz / getrf_test.cu
Created June 8, 2014 00:12
Test of cublasSgetrfBatched
#include <cstdio>
#include <cstdlib>
#include <cuda_runtime.h>
#include <cublas_v2.h>
#define cudacall(call) \
do \
{ \
cudaError_t err = (call); \
if(cudaSuccess != err) \
// an extract from a REPL session
val A = CuMatrix.fromDense(DenseMatrix((1.0f, 1.0f, 2.0f, 1.0f), (1.0f, 2.0f, 1.0f, -2.0f), (3.0f, -1.0f, 3.0f, -2.0f), (-2.0f, 3.0f, -1.0f, 1.0f)))
val N = 4
val Aarray = jcuda.Pointer.to(A.offsetPointer) // this is the one I'm most uncertain about
val P = CuMatrix.create[Int](N, 1) // but I'm not sure about those 2 either
val info = CuMatrix.create[Int](N,1)
JCublas2.cublasSgetrfBatched(handle, N, Aarray, N, P.offsetPointer, info.offsetPointer, 1) // this call returns success (0),
// but A is zeroed-out