Skip to content

Instantly share code, notes, and snippets.

View ymurase's full-sized avatar

Yohsuke Murase ymurase

View GitHub Profile
@ymurase
ymurase / kmeans_pbc.rb
Last active August 29, 2015 14:12
K-means clustering in 2D periodic boundary condition. The length of the system is 1. Calculation of AIC contains a bug (TODO)
require 'pp'
EPSILON = 0.0001
def load_points(io)
points = io.map do |line|
line.chomp.split.map(&:to_f)
end
points
end
@ymurase
ymurase / kmeans_sample_data.rb
Created January 5, 2015 14:53
generate test data for K-means clustering
require 'pp'
unless ARGV.size == 2
$stderr.puts "Usage: ruby #{__FILE__} <n> <expected_k>"
raise "invalid arguments"
end
VARIANCE = 0.05
def draw_gaussian
@ymurase
ymurase / system.cpp
Created December 25, 2014 13:13
call system from c++
#include <iostream>
int main(int argc, char* argv[]) {
std::cout << "starting..." << std::endl;
system("ls");
std::cout << "ls done" << std::endl;
std::cout << "running: " << argv[1] << std::endl;
system(argv[1]);
std::cout << "finished" << std::endl;
return 0;
@ymurase
ymurase / SystemTest.x10
Created December 25, 2014 08:37
x10でsystemを呼ぶ
import x10.io.Console;
import x10.compiler.Native;
public class SystemTest {
public static def main(args:Rail[String]) {
{
@Native("c++", "system(\"ls\");") {}
}
}
@ymurase
ymurase / NativeCppTest.x10
Last active August 29, 2015 14:12
C++で定義された関数を呼ぶ方法。
import x10.io.Console;
import x10.compiler.Native;
import x10.compiler.NativeCPPInclude;
import x10.compiler.NativeCPPCompilationUnit;
@NativeCPPInclude("MyCpp.hpp")
@NativeCPPCompilationUnit("MyCpp.cpp")
public class NativeCppTest {
@Native("c++", "my_double(#1)")
@ymurase
ymurase / Test.x10
Last active August 29, 2015 14:12
x10でNativeのコードを呼ぶ。ミニマム実装
import x10.compiler.Native;
public class Test {
@Native("c++", "printf(\"Hello world.\\n\")")
private static native def test2(): void;
// Use function parameters
// #0 : name of the class (Test)
// #1,#2,#3,...: parameters
@ymurase
ymurase / RunProgram.x10
Last active August 29, 2015 14:11
x10で外部プロセス実行
import x10.io.Console;
import x10.lang.Runtime;
public class RunProgram {
public static def main(args: Rail[String]) {
val times: Long = 10;
for(var k: Long = 0; k< times; k++) {
Console.OUT.println(" I am launching "+ k);
val t = Runtime.execForWrite("sleep 5 && echo " + k);
@ymurase
ymurase / FibG.x10
Last active August 29, 2015 14:11
x10 fibonacci calculation using GLB
import x10.glb.ArrayListTaskBag;
import x10.glb.TaskQueue;
import x10.glb.TaskBag;
import x10.glb.GLBParameters;
import x10.glb.GLB;
import x10.util.Team;
import x10.glb.Context;
import x10.glb.ContextI; // ContextI : Interface for Context
import x10.glb.GLBResult;
@ymurase
ymurase / tcp_client.rb
Created November 15, 2014 08:09
A sample of TCP client implemented by Ruby.
require 'socket'
sock = TCPSocket.open("127.0.0.1", 31400)
5.times do
sock.write("ping\n")
$stdout.puts "sent"
puts sock.gets
end
@ymurase
ymurase / asio_server.cpp
Created November 15, 2014 08:08
A sample of synchronous TCP server using boost::asio
#include <iostream>
#include <boost/asio.hpp>
namespace asio = boost::asio;
std::string readline( asio::ip::tcp::socket & socket ) {
asio::streambuf buf;
asio::read_until( socket, buf, "\n" );
std::string data = asio::buffer_cast<const char*>(buf.data());