Skip to content

Instantly share code, notes, and snippets.

View ugovaretto's full-sized avatar

Ugo Varetto ugovaretto

View GitHub Profile
@ugovaretto
ugovaretto / compute-cuda-blocks.cpp
Created January 10, 2013 12:49
Compute number of CUDA blocks given grid size and number of threads per block.
int compute_blocks(int length, int threads_per_block) {
//integer division:
//if length is evenly divisable by the number of threads
//is equivalent to length / threads_per_block, if not
//it is equivalent to length / threads_per_block + 1
return (length + threads_per_block - 1) / threads_per_block;
}
dim3 compute_blocks(int xsize, int ysize, int zsize,
int threads_per_block_x,
@ugovaretto
ugovaretto / sshfs-osxfuse.txt1
Last active December 11, 2015 17:18
Mac OS X: connect to server through ssh with OSXFuse
1) get OSXFuse: http://osxfuse.github.com
2) create local folder to mount remote fs into
3) invoke sshfs:
sshfs user@server:/home/user/ ~/remote -oauto_cache,reconnect,defer_permissions,negative_vncache,volname=RemoteHome
Look here for additional information:
http://www.read-write.fr/blog/blog/2012/02/26/my-guide-for-sshfs/
@ugovaretto
ugovaretto / direct-function-object-call.cpp
Created January 27, 2013 14:23
Directly invoke a function object as a regular function without having to explicitly construct it
//Author: Ugo Varetto
//
//function objects do not allow a call to the object's operator()(...)
//without an explicit creation:
// struct neg {
// int operator()(int i) const { return -i; }
// };
// neg n;
// std::cout << n(4) << std::endl
// or, if the () operator is const
@ugovaretto
ugovaretto / slurm_node_id
Created March 22, 2013 09:49
Retrieve node id from SLURM
env | grep SLURM_JOB_NODELIST | grep -P -o "\d+"
@ugovaretto
ugovaretto / copy_constructor.cu
Created March 22, 2013 11:24
Test copy constructor within CUDA kernel.
#include <iostream>
#include <cuda_runtime.h>
class CClass {
public:
class Inner {
public:
__host__ __device__ Inner() : value_( 5 ) {}
__host__ __device__ Inner( const Inner& i ) : value_( 9 * i.value_ ) {}
__host__ __device__ int value() const { return value_; }
@ugovaretto
ugovaretto / gl-cl.cpp
Last active December 18, 2015 15:28
Create OpenCL context for OpenGL interoperability
#include <vector>
#include <stdexcept>
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#else
#ifdef WIN32
#include <wingdi.h>
#else
#include <GL/glx.h>
@ugovaretto
ugovaretto / win-gettimeofday.c
Last active March 17, 2024 00:30
gettimeofday implementaiton for windows
/*
* Author: Ugo Varetto - ugovaretto@gmail.com
* This code is distributed under the terms of the Apache Software License version 2.0
* https://opensource.org/licenses/Apache-2.0
*/
#include < time.h >
#include < windows.h >
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
@ugovaretto
ugovaretto / cpp11-localtime.cpp
Created August 20, 2013 09:23
Print local time with C++11
#include <iostream>
#include <iomanip>
#include <chrono>
int main(int, char**) {
std::chrono::time_point< std::chrono::system_clock> tp;
tp = std::chrono::system_clock::now();
const std::time_t t = std::chrono::system_clock::to_time_t(tp);
std::cout << asctime(localtime(&t)) << std::endl;
return 0;
}
@ugovaretto
ugovaretto / cpucycles.cpp
Created January 30, 2014 09:38
Get CPU clock cycles
//FROM STACKOVERFLOW: http://stackoverflow.com/questions/275004/c-timer-function-to-provide-time-in-nano-seconds
inline __int64 GetCpuClocks()
{
// Counter
struct { int32 low, high; } counter;
// Use RDTSC instruction to get clocks count
__asm push EAX
__asm push EDX
@ugovaretto
ugovaretto / variadic-base.cpp
Created May 12, 2014 09:38
Base class initialization with variadic templates
#include <iostream>
struct B1 {
B1(int) {}
int Get() const { return 1; }
};
struct B2 {
B2(int) {}
int Get() const { return 2; }
};