call Kotlin from R

15 May

Steps:

  1. Download & Install kotlin command line compiler
  2. install rJava in R
    (in Ubuntu, you might want to point /usr/lib/jvm/default-java to the jvm you installed)
  3. create a kotlin class, e.g.
    // User.kt
    data class User(name: String, age: Int)
  4. In R
    library(rJava)
    .jinit()
    # add path to User.class and kotlin runtime
    .jaddClassPath(c("./", "/usr/local/kotlinc/lib/kotlin-runtime.jar"))
    .jclassPath()
    tom <- .jnew("User", "Tom", as.integer(20))
    .jcall(tom, "S", "getName")
    .jcall(tom, "I", "getAge")

What computer languages are popular now?

12 Jan

If we order languages by number of Github repositories that are created since 2014/01/01 AND has at least 50 stars:

REPOS LANG
=============
7,783 JavaScript
3,527 Java
2,604 Python
2,422 Objective-C
1,596 Go
1,347 PHP
1,223 Swift
1,118 C++
1,105 C
527 Ruby
============

The future is really in network/mobile software.

– JavaScript will continue to do well at position 1 — every browser runs JavaScript
– Java still has bright future (due to Android and backend services)
– New languages like Go, Swift that are born with features for network/mobile will definitely shine in next 10 years.

Compared this list to TIOBE, the list there looks very very stale.

microservice

30 Jul

Three levels of practices that help learning: retrieval practice, teaching, re-creating

21 Jul

Copied from Scientific American, August 2015:

Retrieval practice does not use testing as a tool of assessment. Rather it treats tests as occasions for learning, which makes sense only once we recognize that we have misunderstood the nature of testing. We think of tests as kind of dipstick that we insert into a student’s head, an indicator that tells us how high the level of knowledge has risen in there — when in fact, every time a student calls up knowledge from memory, that memory changes. Its mental representation becomes stronger, more stable and more accessible.

Why would this be? It makes sense considering that we could not possibly remember everything we encounter, says Jefferey Karpicke, a professor of cognitive psychology at Purdue University. Given that our memory is necessarily selective, the usefulness of a fact or idea — as demonstrated by how often we have had reason to recall it — makes a sound basis for selection. “Our minds are sensitive to the likelihood that we’ll need knowledge at a future time, and if we retrieve a piece of information now, there’s a good chance we’ll need it again,” Karpicke explains. “The process of retrieving a memory alters that memory in anticipation of demands we may encounter in future.”

Comment:
“retrieval practice” is simple, to use it wisely, teachers should design test that needs to retrieval the most important things. In elemental/high school, I hated test questions in history class that ask me to give exact date of a not-that-important event. I would give score D to all history teachers who do such things in class.

We all know that the best way to know something is to teach it. This is a much higher level memory practice than the simple retrieval practice. To teach something, you actually need to organize the knowledge and present it in a way that is easy to follow. It is no wondering teaching something is much more effective than retrieval practice.

An even higher level of practice is “re-creating”, e.g to try to prove a math lemma/law before you see the proof in textbook, to ask yourself why we need the concept after we read the definition of a math concept, to try to put your foot in the shoes’s of whoever created the concepts/theories — how would you solve the problem if you were him/her/them.

Update: after search online, I think this article from Time Magazine worth reading: The Protégé Effect

Run CUDA on Ubuntu 14.04 (Desktop)

19 Jul

What is CUDA?

Modern video card on desktop/laptop can be used for general parallel computing. CUDA is such a platform developed by Nivida. On my laptop, the Nvidia card has 96 CUDA cores. A desktop Nvidia card can easily have 1000 CUDA cores. For the purpose of simplicity, you can think of one CUDA core as one CPU core when doing some parallel math computations.

Setup CUDA 7 on Ubuntu 14.04 (Desktop)

My laptop has two video cards: one is intel HD, the other is Nvidia 5200M. Ubuntu uses the Intel card for GUI, Nvidia card is not activated. After quite a few search & try, I setup CUDA on my laptop. Below is the steps that works for me.

Hardware: Thinkpad T430s two video cards: Intel & Nvidia NVS 5200M
OS: Ubuntu 14.04 (Desktop)

  • Remove all Nvidia default driver in Ubuntu
  • Create a file at /etc/modprobe.d/blacklist-nouveau.conf with the following contents:
blacklist nvidiafb
blacklist nouveau
blacklist rivafb
blacklist rivatv
blacklist vga16fb
options nouveau modeset=0

Regenerate the kernel initramfs: sudo update-initramfs -u

  • Remove all other Nvidia packages:
    sudo apt-get purge nvidia*
  • Search all packages related to nouveau: dpkg –get-selections | grep -v deinstall|grep -i nouveau, you might have to sudo apt-get purge xserver-xorg-video-nouveau-lts-vivid
  • Sometime you must run sudo update-initramfs -u so the initramfs can be purged of nouveau
  • Download CUDA 7 toolkit here, and then reboot Ubuntu to a text/console mode (basically hold shift key and then edit with ‘e’ and continue boot with ctrl-x. For detail help, see this), and then run the downloaded file:
$ sudo sh cuda__version__linux.run
  • When prompt, install Nvidia driver
  • When prompt, remember NOT to install OpenGL driver: I mistakenly installed OpenGL and got blank GUI when reboot.
  • Install CUDA toolkit & examples & reboot
  • verify your installation:
$ cd
$ cd 1_Utilities/deviceQuery
$ make
$ ./deviceQuery
./deviceQuery Starting...

CUDA Device Query (Runtime API) version (CUDART static linking)

Detected 1 CUDA Capable device(s)

Device 0: &amp;quot;NVS 5200M&amp;quot;
CUDA Driver Version / Runtime Version 7.0 / 7.0
CUDA Capability Major/Minor version number: 2.1
Total amount of global memory: 1024 MBytes (1073479680 bytes)
( 2) Multiprocessors, ( 48) CUDA Cores/MP: 96 CUDA Cores
GPU Max Clock rate: 1250 MHz (1.25 GHz)

What does not work

The nvidia-346 and nvidia-340 drivers in ppa:xorg-edgers/ppa do NOT work.

What if cuda does not work after laptop wake up from sleep

put following 2 lines in your /etc/rc.local

/usr/bin/nvidia-smi -pm ENABLED
/usr/bin/nvidia-smi -c EXCLUSIVE_PROCESS

New features for Go

5 Jun

Below are the features I think would be really cool to add to Go.

1. User can re-define operators like +, >, .. for a struct

2. Support list comprehension: x = [e for e in y if e > 0]

3. Support shorthand function definition, like scala:  (x: Double) => x * x

4. Support type inference or pattern match for functions. For example, suppose method X accepts a func (x, y int) as one of its parameters, we can call it this way:

a.X(func (x, y) { ….}, …), i.e. without specifying the types of the parameters x and y in the anonymous function — they are automatically inferred.

5. generic

Security for personal linux servers

31 May

A few days ago I used a Raspberry Pi 2 as a toy server. Yesterday I noticed that it already got 72,000 failed SSH logins, this prompted me to search some linux security tips. This site ubuntu hardening guide is the one I would recommend. Below is a summary of what I learned (from reading the mentioned site and other pages)

0. sudo apt-get update && sudo apt-get dist-upgrade  (also remember to setup auto update).

This would keep your server updated (and hence less vulnerable to known bugs).

1. Setup firewall to only allow the ports (like ssh/22, http/80, https/443) that you plan to use, and put rate limit on SSH port.

a) sudo ufw allow ssh

b) sudo ufw limit ssh/tcp

2. Install fail2ban: to ban an IP if there are too many failed login in a short time from that ip.

3. Disable SSH login via password, always pick SSH login via keys (detail), and only allow certain users to login via ssh. A poor ssh password is a big risk. On the other hand, ssh login via key is much safer — It’s much much more harder (if not impossible) to brute force ssh keys.

4. Remove not-needed packages and/or disable not-needed services:  these packages or services might hide security bugs.

5. Lock and/or make some routine users (like root, irc, ..) not able to login (here)

6. Monitoring & read ubuntu hardening guide