Hello world from device+PYCUDA - printf

I am starting to learn PyCUDA on Google Colab. I’m trying to run the "printf" example.
Everything works fine, but I do not get any output on the last line. How can I solve it?
import pycuda.driver as drv
import pycuda.autoinit
from pycuda.compiler import SourceModule
mod = SourceModule("""
#include <stdio.h>
__global__ void myfirst_kernel()
{
printf("Hello,PyCUDA!!!");
}
""")
function = mod.get_function("myfirst_kernel")
function(block=(4,4,1))
# Flush context printf buffer
cuda.Context.synchronize()

drv.Context.synchronize()
just make this change, it will work now.

Related

PyQt5 set qt_ntfs_permission_lookup

I want to use isWritable() from QFileInfo. According to the docs, you have to somehow set qt_ntfs_permission_lookup to 1 to get a meaningful result on Windows. The C++ code for this is
extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;
qt_ntfs_permission_lookup++; // turn checking on
qt_ntfs_permission_lookup--; // turn it off again
How do I "translate" the extern statement into Python?
One possible solution is to create functions that change the state of that variable in C++ and export it to python. To export a C++ function to python there are options like pybind11, SWIG, sip, shiboken2, etc.
In this case, implement a small library using pybind11
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
namespace py = pybind11;
#ifdef Q_OS_WIN
QT_BEGIN_NAMESPACE
extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;
QT_END_NAMESPACE
#endif
PYBIND11_MODULE(qt_ntfs_permission, m) {
m.def("enable", [](){
#ifdef Q_OS_WIN
qt_ntfs_permission_lookup = 1;
#endif
});
m.def("disable", [](){
#ifdef Q_OS_WIN
qt_ntfs_permission_lookup = 0;
#endif
});
#ifdef VERSION_INFO
m.attr("__version__") = VERSION_INFO;
#else
m.attr("__version__") = "dev";
#endif
}
and you can install it by following these steps:
Requirements:
Qt5
Visual Studio
cmake
git clone https://github.com/eyllanesc/qt_ntfs_permission_lookup.git
python setup.py install
Also with the help of github actions I have created the wheels for some versions of Qt and python so download it from here, extract the .whl and run:
python -m pip install qt_ntfs_permission-0.1.0-cp38-cp38-win_amd64.whl
Then you run it as:
from PyQt5.QtCore import QFileInfo
import qt_ntfs_permission
qt_ntfs_permission.enable()
qt_ntfs_permission.disable()

Can not resolve JavaParser

JavaParser.CallContext and JavaParser.FunctionDeclContext do not seem to be able to resolve. This is modeled after page 139 in the definitive antlr reference.
Am I missing a Lib?
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.misc.MultiMap;
import org.antlr.v4.runtime.misc.OrderedHashSet;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.*;
import org.stringtemplate.v4.ST;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Set;
static class FunctionListener extends JavaBaseListener {
Graph graph = new Graph();
String currentFunctionName = null;
public void enterFunctionDecl(JavaParser.FunctionDeclContext ctx) {
currentFunctionName = ctx.ID().getText();
graph.nodes.add(currentFunctionName);
}
public void exitCall(JavaParser.CallContext ctx) {
String funcName = ctx.ID().getText();
// map current function to the callee
graph.edge(currentFunctionName, funcName);
}
}
I think I have seen this one, and I dont have enough points to "comment", but let me ask a question first; It looks like you are trying to produce an external version of the AST on page 137, You took the examples and renamed them to the grammar you had generated already. I'm going to assume that the rest of it is working or you would have had a lot more errors than this one ! Is that the goal ? Are you after just the calling methods/classes or are you after the full homogeneous AST ?
This depends on the grammar entry points. That's not as obvious in the book as it seems. You referenced functionDecl, which looks to be an entry in the Cymbol.g4, but doesn't exist in Java.g4 So rather than JavaParser.FunctionDeclContext I suggest JavaParser.classOrInterfaceDeclarationContext. It should should pull up the right method. I will also confess that I don't know what the exitCall would map to. I could use the illumination on that myself.
Were you after the whole AST or only the call graph ? If the whole AST, I think you can use enterEveryRule or ExitEveryRule for that as well, but confirmation would be good.
So start by regenerating your grammar, change your program to reference the rule entry point in the .g4 files, then see if it all works.
Thanks

Having trouble with Random ore generation for minecraft 1.15.2

So I am trying to make a minecraft mod that has a randomly generated ore. I have run into a problem in this part of the code.
import net.minecraft.world.biome.Biome;
import net.minecraft.world.biome.Biomes;
import net.minecraft.world.gen.GenerationStage;
import net.minecraft.world.gen.feature.Feature;
import net.minecraft.world.gen.feature.OreFeatureConfig;
import net.minecraft.world.gen.placement.ConfiguredPlacement;
import net.minecraft.world.gen.placement.CountRangeConfig;
import net.minecraft.world.gen.placement.Placement;
import net.minecraftforge.registries.ForgeRegistries;
public class ModOreGen {
public static void generateOre() {
for (Biome biome : ForgeRegistries.BIOMES) {
if (biome == Biomes.BAMBOO_JUNGLE) {
ConfiguredPlacement<CountRangeConfig> customConfig = Placement.COUNT_RANGE
.func_227446_a_(new CountRangeConfig(9, 10, 10, 0));
biome.addFeature(GenerationStage.Decoration.UNDERGROUND_ORES,Feature.ORE.withConfiguration(new OreFeatureConfig(OreFeatureConfig.FillerBlockType.NATURAL_STONE, blockinit.chocolate_ore.getDefaultState(), 10)).withPlacement(customConfig));
}
}
}
}
Where it says .withConfiguration it gives me the error:
The method withConfiguration(OreFeatureConfig) is undefined for the type Feature<OreFeatureConfig>
I have already tried updating my mappings and such, but nothing helped. This has been a problem that has really irritated me for days now. What is happening?
I just had this same problem with my code and finally fixed it. Try this out!
import net.minecraft.world.biome.Biome;
import net.minecraft.world.biome.Biomes;
import net.minecraft.world.gen.GenerationStage;
import net.minecraft.world.gen.feature.Feature;
import net.minecraft.world.gen.feature.OreFeatureConfig;
import net.minecraft.world.gen.placement.ConfiguredPlacement;
import net.minecraft.world.gen.placement.CountRangeConfig;
import net.minecraft.world.gen.placement.Placement;
import net.minecraftforge.registries.ForgeRegistries;
public class ModOreGen {
public static void generateOre() {
for (Biome biome : ForgeRegistries.BIOMES) {
if(biome == Biomes.BAMBOO_JUNGLE) {
ConfiguredPlacement<?> customConfig = Placement.COUNT_RANGE
.configure(new CountRangeConfig(9, 10, 10, 0));
biome.addFeature(GenerationStage.Decoration.UNDERGROUND_ORES, Feature.ORE.withConfiguration(newOreFeatureConfig(OreFeatureConfig.FillerBlockType.NATURAL_STONE,BlockInit.chocolate_ore.getDefaultState(), 10)).withPlacement(customConfig));
}
}
}
}
The obvious thing Sammerson did was to remove the strong typing for CountRangeConfig:
ConfiguredPlacement<?> , but that doesn't matter.
What you can't see is updating Forge to 1.15.2.
This is most likely your best fix. In your build.gradle, somewhere near the top (mine is line 28) you've probably already updated your mappings to:
mappings channel: 'snapshot', version: '20200409-1.15.1'
But you also want to go down and update the Forge version also (this is around line 90 for me).
dependencies {
minecraft 'net.minecraftforge:forge:1.15.2-31.1.0'
}
You need to do the same
gradlew genEclipseRuns
gradlew eclipse
just like updating the mappings.
(You can check the Forge page, there may be a newer version than 1.15.2 by the time someone else reads this. And I hope anyone using IntelliJ can figure out how to update your own mappings/forge.))

Load ONNX CNTK C++ raises exception

I have one file that I created in matlab. I used it very well in
python load:
import cntk as C
z = C.Function.load("Net.onnx", format=C.ModelFormat.ONNX)
in c++ I have exception Selected CPU as the process wide default device.
About to throw exception:
'Gemm: Invalid shape, input A and B are expected to be rank=2
matrices'
I used the nuget imported : CNTK.CPUOnly CNTK.Deps.MKL CNTK.Deps.OpenCV.Zip
#include <stdio.h>
#include "CNTKLibrary.h"
void main(){
std::wstring modelFile(L"Net.onnx");
//line crash
CNTK::FunctionPtr modelFunc = CNTK::Function::Load(modelFile, CNTK::DeviceDescriptor::CPUDevice(), CNTK::ModelFormat::ONNX);
}
finaly i made other solution i save in python to model cntk than loaded it from c++
in cntk format (where original model was exported from matlab to onnx long way)
python code
import cntk as C
z = C.Function.load("Net.onnx", format=C.ModelFormat.ONNX)
z.save(os.path.join("folder", "net" + ".dnn"))
c++ loading
#include "CNTKLibrary.h"
std::wstring modelFile(L"net.dnn");
CNTK::FunctionPtr modelFunc = CNTK::Function::Load(modelFile, CNTK::DeviceDescriptor::CPUDevice());

What are your favorite skeleton files for the various languages? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
It is useful to have skeleton or template files that you can just copy and use as a basis for a new script or app.
For example, I use the following ones (emacs with the auto-insert module automatically opens a copy of the appropriate skeleton file when I create a new file).
Perl:
#!/usr/bin/perl -w
use strict;
use Getopt::Long;
my $verbose = 1;
GetOptions("verbose!" => \$verbose
) or die("options error");
C++:
#include <iostream>
#include <stdexcept>
int main(int argc, char** argv){
try{
}
catch(std::exception& e){
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Arguably, one could include basic code for boost::program_options etc.
What are your favorite skeleton files?
The only skeleton file I have is for LaTeX.
\documentclass{article}
%\documentclass[11pt]{amsart}
\usepackage[dvips]{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{cancel}
\oddsidemargin0cm
\topmargin-1cm
\textwidth16.5cm
\textheight23.5cm
\parskip1ex
\parindent0ex
\begin{document}
\title{ ... }
\author{ ... }
\date{ ... }
\maketitle
\end{document}
Obviously I use this for writing math papers.
Otherwise, I always start from scratch. There's no programming language I can think of where the requisite infrastructure is more than you can keep around in your brain or take longer than 20 seconds to type out.
My Perl templates look like this:
If I am opening a .pm module:
use MooseX::Declare;
class What::Ever {
};
1;
Or, if not on a MooseX::Declare project:
package What::Ever;
use Moose;
1;
If it's a .pl file:
#!/usr/bin/env perl
use strict;
use warnings;
use feature ':5.10';
Since I use autoinsert.el, I also have it ask me if I want to use FindBin; if so:
#!/usr/bin/env perl
use strict;
use warnings;
use feature ':5.10';
use FindBin qw($Bin);
use lib "$Bin/../lib";
The necessary emacs code is in my elisp repository at http://github.com/jrockway/elisp/blob/fd5d73530a117a13ddcde92bc1c22aba1bfde1f0/_local/auto-inserts.el.
Finally, I think you will prefer MooseX::Getopt to plain Getopt. It is a much more maintainable approach to writing "one-off" scripts. (The next few lines go something like:
use My::Script; # that's why we did the "use lib" thing
My::Script->new_with_options->run; # this parses the command line, creates a new object, and runs the script
All the important code goes in a class that can be unit tested, glued to a web app, etc.)
In visual studio, they're called Project files; my current favorite is Windows Application ;-)
Java
package edu.vt;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Template
{
private Log log = LogFactory.getLog(getClass());
/* Constructors
***************************************************************************/
public Template()
{
}
/* Accessors/Mutators
***************************************************************************/
/* Local Methods
***************************************************************************/
}
and
package testing.edu.vt;
import edu.vt.Template;
import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class TemplateTestCase extends TestCase
{
private final Log log = LogFactory.getLog(getClass());
public TemplateTestCase(final String name)
{
super(name);
}
protected void setUp()
{
}
protected void tearDown()
{
}
public void testLifeCycle() throws Exception
{
assertTrue(true);
}
}
Python is simple, but it still helps if you import things with shortcut names, for example:
import sys
import numpy as np
import pylab as pyb
import matplotlib.pyplot as plt
import matplotlib as mpl
But just don't do: import skynet.
Bourne Shell
#!/bin/sh
usage() {
cat <<EOF
$0 <cmd>
cmd:
samplecmd
EOF
}
cmd=${1}
shift
case ${cmd} in
samplecmd)
arg1=${arg1:-${1}} # arg list takes precedence over env var
if [ "x${arg1}" = "x" ] ; then
usage
fi
samplecmd ${arg1}
;;
*)
usage
;;
esac
I like to make little helper scripts like this to document commands I type in the shell.
When I'm writing code that will be OSS I have a simple boiler plate template that I can key in the license and URL to the license text. The boiler plate has author details, and other crap hard coded.
For commercial dev I have a boiler plate with company information, and standard copyright notices in it.
I don't keep any standard skeletons because I've found I just cut out the content and added my own anyway. Most cases are different enough that changing the skeleton to match takes as long as bashing it out by hand.