function write_mat_to_binary(M,fname)
	fprintf('writing matrix to %s\n',fname);
	m = size(M,1); n = size(M,2);
	fp = fopen(fname,'w');
	fwrite(fp,m,'int32');
	fwrite(fp,n,'int32');
	% write nnzs, column major format 
	for j=1:n
		fprintf('writing column %d of %d\n', j,n);
		for i=1:m
			fwrite(fp,M(i,j),'double');
		end
	end
	fclose(fp);
end
